Starting the same process >1

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]
LVL 9
DrWarezzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrWarezzAuthor Commented:
You're the man petmagdy -- it works :-)

Thanks alot! ;-)
[r.D]
0
petmagdyCommented:
welcome :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.