close external program

Hello, I've created this simple chat program with swing. And Ive also made this external server which handels the klients(users). Ive turned this server program into an .exe file. In my chat program I can start this server with:

Runtime runtime = Runtime.getRuntime();

Process p = runtime.exec("c:\\searchwayToWereMyServerIs.exe");

And that works fine.

But now I wanna close the server also. Ive tryed to use the p.destroy();

command but it doesnt work. Does anyone know an way(another way) to end this external server?

[583 byte] By [Dingo_no_1a] at [2007-11-26 14:49:46]
# 1
I think there is a p.WaitForClose() or something but I don't think you can send a close signal to the app yourself (I may be wrong).your options to communicate with a external app include: - socketssockets is probably the best i can think of ...
prob.not.sola at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 2
> But now I wanna close the server also. Ive tryed to> use the p.destroy(); command but it> doesnt work. What do you mean it doesn't work?
cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 3
That command doesnt exisist :(. But there is nothing wrong with the server though. It works as it should and I can chat with friends with the program. I just cant shut down the server without using the taskmanager..
Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 4
What command doesn't exist?
cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 5

I mean that nothing happens. Its still working after Ive used that command.

Im my chat program where I start it I use menues and therefore actionlisteners to start the server, this is how it looks:

serverAction.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {

try {

process = Runtime.getRuntime().exec(serverPath); // start a process

}

catch (IOException e) {

display.setText("Could not start server");

}

}

});

And this is the listener for the close server:

closeServerAction.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {

process.destroy();

}

});

Is it wrong to do it like that?

Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 6
I'd like to see the actual command you use to start this server. Please post that.
cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 7
The suggested command above in reply 2: p.WaitForClose().
Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 8
> The suggested command above in reply 2:> p.WaitForClose().No. I want to see the actual command you use to start the server. Not the Java code but what you are calling through exec.
cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 9
> The suggested command above in reply 2:> p.WaitForClose().i believe that is actually for c#. ignore that reply.
shoopy.a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 10

This is the actuall start sequense:

String serverPath = "\"C:\\Documents and Settings\\P B\\workspace\\Java 錵2\\labb3\\server.exe\"";

Process process = Runtime.getRuntime().exec(serverPath);

But its nothing wrong with that, cause the server starts, its closing it that is the problem.. :/

Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 11

> > The suggested command above in reply 2:

> > p.WaitForClose().

>

> i believe that is actually for c#. ignore that reply.

Well there is a waitFor() method of Process but I don't believe that is what the OP is looking for.

I suspect that the other program is a service and needs a proper stop service command to be issued but I don't know because the OP won't tell me.

cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 12

> This is the actuall start sequense:

>

> String serverPath = "\"C:\\Documents and

> Settings\\P B\\workspace\\Java

> 錵2\\labb3\\server.exe\"";

>

> Process process =

> Runtime.getRuntime().exec(serverPath);

>

>

> But its nothing wrong with that, cause the server

> starts, its closing it that is the problem.. :/

*sigh*

server.exe is some sort of wrapper that is actually firing off your program. Do you know what the real program is?

cotton.ma at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 13
In reply to 11. What else do I need to tell. It磗 an .exe file that works as an external program(its independent of the programs using it)..
Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 14
No.. Im getting confused now.. :( No I dont know what the real program is.. Do you mean the chatprogram that is using the .exe program..? Sry for being such a noob...
Dingo_no_1a at 2007-7-8 8:37:49 > top of Java-index,Java Essentials,Java Programming...
# 15

> In reply to 11. What else do I need to tell. Its an

> .exe file that works as an external program(its

> independent of the programs using it)..

Again. That server.exe is some sort of wrapper that is firing off your actual program. Thus when you call destroy you are killing it (the wrapper which frankly may not even be running at that time) but not the actual program (started as a process from server.exe).

So you need to either

a) identify the actual program and interact with it appropriatley

or

b) interact with the wrapper program appropriately

cotton.ma at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 16

> No.. Im getting confused now.. :( No I dont know what

> the real program is.. Do you mean the chatprogram

> that is using the .exe program..? Sry for being such

> a noob...

what he means is server.exe is a shell for calling the real program. what will happen is server.exe will run and then close while your main program is still running.

this is causing your problem.

shoopy.a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 17

>So you need to either

>a) identify the actual program and interact with it appropriatley

>or

>b) interact with the wrapper program appropriately

How do I do that? Ive just noticed that in the taskmanager its called: javaw.exe. How do I interact with it...

Message was edited by:

Dingo_no_1

Dingo_no_1a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 18
> How do I do that? Ive just noticed that in the> taskmanager its called: javaw.exe. How do I interact> with it...what you need to do is run the java application yourself, runtime.execute("java MyClass"), then you should be able to end it with .destroy().
shoopy.a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 19
> How do I do that? Ive just noticed that in the> taskmanager its called: javaw.exe. How do I interact> with it...Why are you even creating a new process for this other application? Why not wrap it up as a library, and include it in the same process?
mlka at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 20
Because I wanna learn how to do it. For future use.
Dingo_no_1a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 21

If you're launching a process via Runtime.exec() on Windows through a subshell (that is, not "myprog.exe" but "cmd /c myprog.exe"), then destroy() won't kill the spawned program. It would if you just launched "myprog.exe". If your app is also launching something external, find out what it is and kill it the way you would in windows command interpreter: taskkill /F /IM "filename.exe" (or "*iexplore*.*" ;) By that I mean calling another Runtime.getRuntime().exec("taskkill ....");

HTH.

owczi.a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 22

thanx for answere but I cant get it to work either. I only get IOExceptions when doing that :(.

BTW In my other topic about the same Ive discovered that with normal .exe files like notepad it works to use the destroy() metod, But with my own created .exe file it wont work. This is my latest and most important add in that topic:

(add from other topic so the discussion can continue here)

Ok, Ive tryed it out now. I made an easy example;

import java.io.IOException;

public class Test {

private static Processp;

public static void main(String[] args){

try {

p = Runtime.getRuntime().exec("\"C:\\Windows\\notepad.exe\"");

} catch (IOException e) {

e.printStackTrace();

}

try {

Thread.sleep(3000);

p.destroy();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

This works great using the p.destroy(); which I just noticed >_<. The thing is that then the .exe file I make my self and try to destroy just wont work :(.

This is how my own .exe file looks like(really simple just as an example):

import javax.swing.JFrame;

public class testprogram {

public static void main(String[] args){

JFrameframe = new JFrame("Testframe");

frame.setSize(200, 0);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

When I make this into an exe file using java launcher 3.2 I get the problem that I can start it but cannot close it with destroy(). Does anyone know why this little .exe file wont be closed with destroy()? I mean, is something supposed to be added to the code so that it can be closed? please help.

Dingo_no_1a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 23

Once again the exe making program is simply wrapping your Java program. Meaning it works like this.

exe -> starts new process with javaw

When you call the exe from Java it does this

your program -> starts new process with exe -> starts new process with javaw

Now your Java program only knows about the process it started, the exe. Not the process started by the process it started (javaw). So when you call destroy you are destroying the exe and NOT the (or any) processes the exe spawned.

cotton.ma at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 24
So destroy is working, just as you noted with your notepad example. But it isn't getting killing processes spawned from the process you started.There is no direct Java resolution for this issue.
cotton.ma at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 25
Ok, but how can it work when I launch notepad then instead? Also can you give me a hint on how to destroy the process created by the process? Or is it nativ programming for that(C/C++)?
Dingo_no_1a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 26

> Ok, but how can it work when I launch notepad then

> instead? Also can you give me a hint on how to

> destroy the process created by the process? Or is it

> nativ programming for that(C/C++)?

It's not really even a question of using C++.

At it's heart I suspect it's more of an OS related issue. I think.

Anyway there are two ways around it (which I have mentioned previously in this thread)

1) Your exe may have a way to recall it or send a switch or something so that the process is killed. I kind of doubt it. This is the way service wrappers work (and they do exist) but your program does not seem to be a service

2) Skip the exe altogether. Start your other Java program from your first Java program directly. Then you can kill it.

cotton.ma at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 27
Can you explain where notepad do get its killing processes from then? I mean cant something like that be added to my java code then so it gets it own killing processes like you described notepad does..
Dingo_no_1a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 28

> Can you explain where notepad do get its killing

> processes from then? I mean cant something like that

> be added to my java code then so it gets it own

> killing processes like you described notepad does..

Look.

In your situation you are starting TWO processes.

1) the launcher exe

2) the javaw.exe RUNNING YOUR JAVA APP

3) your main program

your main program (3) runs 1, which runs 2. 3 then attempts to close process 1. BUT IT ISNT RUNNING. 3 does not have a way to close 2.

do you get it? you have THREE processes. you are only able to close the first one via p.destroy().

The solution is very very simple: STOP using the launcher process.

shoopy.a at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 29

> Can you explain where notepad do get its killing

> processes from then? I mean cant something like that

> be added to my java code then so it gets it own

> killing processes like you described notepad does..

Okay please note the number of -> in the following.

Scenario A

Your Java program -> starts notepad

Notepad process can be destroyed

Scenario B

Your Java program -> starts other Java program

other java program process can be destroyed

Scenario C

Your Java program -> starts exe -> starts other Java program

exe process can be destoyed. Other Java program CANNOT be destroyed

Scenario D

Your Java program -> starts other Java program-> starts exe

Other Java program can be destroyed. exe CANNOT be destroyed.

cotton.ma at 2007-7-21 16:19:05 > top of Java-index,Java Essentials,Java Programming...
# 30

ok, ty for reply 26. about the second suggestion; I cant, Ive tryed that, but as in earlier replyes said, its not that simple frame, but an server for an chat program, which is running threads. One new thread is started for each klient connecting to the server. And those threads are then continuing in a while loop so the server keeps responding on inputs made from klients(the chatter) so the other klients can get the mssg klient1 sent. So when I try to start the server by calling it like it was an normal program the program hangs it self in the while(true) loop ofc. So I cant do it like that.

Dingo_no_1a at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 31
in reply to 28. Ty that made it cristal clear :).
Dingo_no_1a at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 32

> ok, ty for reply 26. about the second suggestion; I

> cant, Ive tryed that, but as in earlier replyes said,

> its not that simple frame, but an server for an chat

> program, which is running threads. One new thread is

> started for each klient connecting to the server. And

> those threads are then continuing in a while loop so

> the server keeps responding on inputs made from

> klients(the chatter) so the other klients can get the

> mssg klient1 sent. So when I try to start the server

> by calling it like it was an normal program the

> program hangs it self in the while(true) loop ofc. So

> I cant do it like that.

I really don't understand your problem description at all.

Have you thought about wrapping this server program as a service? It might solve your problem, I say might because I really don't understand what the problem is.

cotton.ma at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 33
For example look at this http://wrapper.tanukisoftware.org/doc/english/introduction.html
cotton.ma at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 34
No I have not though of making it into an service.. Cause Im a noob lol. Can you explain how to do that and how to use it? I dont think I get what an service is either :/
Dingo_no_1a at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 35

> No I have not though of making it into an service..

> Cause Im a noob lol. Can you explain how to do that

> and how to use it? I dont think I get what an service

> is either :/

*hand wave*

you don't want a service. you just want to stop using the launcher executable.

shoopy.a at 2007-7-21 16:19:09 > top of Java-index,Java Essentials,Java Programming...
# 36

> > No I have not though of making it into an

> service..

> > Cause Im a noob lol. Can you explain how to do

> that

> > and how to use it? I dont think I get what an

> service

> > is either :/

>

> *hand wave*

>

> you don't want a service. you just want to stop using

> the launcher executable.

I agree except he said before that he can't. I mentioned this in reply 26 and he said he can't with a indeceipherable explanation of why he can't.

cotton.ma at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 37
lol, yes. The reason I cant is cause the program will hang it self if I do, due to while loops I have running for infinity. Which they much.. I think.
Dingo_no_1a at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 38
> lol, yes. The reason I cant is cause the program will> hang it self if I do, due to while loops I have> running for infinity. Which they much.. I think.:(This doesn't make any sense at all.
cotton.ma at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 39

> > lol, yes. The reason I cant is cause the program

> will

> > hang it self if I do, due to while loops I have

> > running for infinity. Which they much.. I think.

>

> :(

>

> This doesn't make any sense at all.

i reckon he's saying his server is in a constant loop, and without putting in in an external program it will always run. i.e. he needs to learn about threads.

shoopy.a at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 40
I didnt think it would :/. Just to bad I dont got the code for it on this computer, cause then I could have posted it and you would see for your self. But I will do it tommorow and it will probably get more clear..
Dingo_no_1a at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 41

> > > lol, yes. The reason I cant is cause the program

> > will

> > > hang it self if I do, due to while loops I have

> > > running for infinity. Which they much.. I think.

> >

> > :(

> >

> > This doesn't make any sense at all.

>

> i reckon he's saying his server is in a constant

> loop, and without putting in in an external program

> it will always run. i.e. he needs to learn about

> threads.

But what does that have to do with anything.

His current configuration

Java -> exe -> his looping server

We keep telling him to do this

Java -> his looping server

And his response is I can't because the server is in an infinite while loop?!?

I don't doubt that it is but I don't see what that has to do with how it gets started (or destroyed) and why it seems to make the exe neccessary (it doesn't)

cotton.ma at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 42

> But what does that have to do with anything.

>

> His current configuration

>

> Java -> exe -> his looping server

>

> We keep telling him to do this

>

> Java -> his looping server

>

> And his response is I can't because the server is in

> an infinite while loop?!?

i guessed he was thinking new prog + looping server (i.e. in the same class or whatever).

shoopy.a at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...
# 43

>I don't doubt that it is but I don't see what that has to do with how it gets started >(or destroyed) and why it seems to make the exe neccessary (it doesn't)

I needed the exe before cause the program stopped working if I used the server directly.

But I start to think that I only need to learn more on how to make threads and how to handle them.. which do make the exe useless. But at the time I wanted an easy way out so I made it into an .exe.

Dingo_no_1a at 2007-7-21 16:19:10 > top of Java-index,Java Essentials,Java Programming...