[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Starting the same process >1

Posted on 2004-11-28
3
Medium Priority
?
237 Views
Last Modified: 2010-03-31
Hi all,

I've created the followig program to demonstrate this:

-- RunApp.java

import java.io.*;
import java.awt.*;
import java.util.*;

public class RunApp {
   
    public static void main ( String [] args ) {
       
        RunApp ra = new RunApp();
       
        for ( int i=0; i<3; i++ ) {
            ra.startIt();
            ra.endIt();
        }
       
    }
   
    public void startIt () {
       
        System.out.println( "Starting ..." );
       
        try {
            Process child = Runtime.getRuntime().exec( "c:\\windows\\notepad.exe" );
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {Thread.sleep( 2000 );} catch (Exception e) {}
    }
   
    public void endIt () {
       
        System.out.println( "Ending ..." );
       
        try {
            Process child = Runtime.getRuntime().exec( "c:\\downloads\\temp\\pskill\\pskill.exe NOTEPAD.EXE" );
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }
   
}


Basically, when I run it, it should open notepad, pause for 2 seconds, close notepad, then repeat that, two more times.

However, it will happily open and close it the first time, but after that, the other two attempts, it doesn't open notepad at all (although no exception occurs)!?

Anyone know what this is about??
Can I alter the code to allow it to work properly?!

Thanks!
[r.D]
0
Comment
Question by:DrWarezz
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12690334
try this:

    import java.io.*;
    import java.awt.*;
    import java.util.*;

    public class RunApp {
        Process child;
        public static void main ( String [] args ) {
           
            RunApp ra = new RunApp();
           
            for ( int i=0; i<3; i++ ) {
                ra.startIt();
                ra.endIt();
            }
           
        }
       
        public void startIt () {
           
            System.out.println( "Starting ..." );
           
            try {
                child = Runtime.getRuntime().exec( "c:\\windows\\notepad.exe" );
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {Thread.sleep( 2000 );} catch (Exception e) {}
        }
       
        public void endIt () {
           
            System.out.println( "Ending ..." );
           
            try {
                child.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
           
        }
       
    }
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12690359
You're the man petmagdy -- it works :-)

Thanks alot! ;-)
[r.D]
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12690370
welcome :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question