Stopping a Java Program

T

TheRhythmic

Hi everyone,

This' the issue am facing:

A java application is runs forever. I want to provide the users a
facility to shut it down whenever they want to. How to do this?

I tried writing another class, say class A, which does this:
Runtime r = Runtime.getRuntime();
r.exit(o);

I started the App. Then when I wanted to shut it down, I executed class
A's method by using the 'java' command. But this doesn't seem to help
in stopping the App. A's method executes successfuly, but I think it
starts a new JVM & executes in it & so the exit() command doesn't have
effect on the JVM in which the App is running.

One more method I thought of is to execute a method in the same JVM &
do an 'exit(0)' to terminate the JVM so that App will shut down. But
donno how this'll be possible because 'java' command starts a new JVM.

Please give your suggestions on what can be done.

Thanks.
 
E

EdwardH

One more method I thought of is to execute a method in the same JVM &
do an 'exit(0)' to terminate the JVM so that App will shut down. But
donno how this'll be possible because 'java' command starts a new JVM.

System.exit(0);

I don't see what the problem should be.
 
S

Simon

A java application is runs forever. I want to provide the users a
facility to shut it down whenever they want to. How to do this?

I tried writing another class, say class A, which does this:
Runtime r = Runtime.getRuntime();
r.exit(o);

I started the App. Then when I wanted to shut it down, I executed class
A's method by using the 'java' command. But this doesn't seem to help
in stopping the App. A's method executes successfuly, but I think it
starts a new JVM & executes in it & so the exit() command doesn't have
effect on the JVM in which the App is running.

One more method I thought of is to execute a method in the same JVM &
do an 'exit(0)' to terminate the JVM so that App will shut down. But
donno how this'll be possible because 'java' command starts a new JVM.


I assume you don't have access to the programs source code. You can try the
following:

Find the main class of the application, say app.Main, put the application and
your class A on the classpath and do the following.

* start app.Main.main() in a new thread from A.main()
* Wait for the user action that is supposed to terminate the app.
* Exit the JVM

like this:

public class A {

public static void main(final String[] argv) {
new Thread() {
public void run() {
app.Main.main(argv);
}
}.start();
waitForNotificationAboutUserAction();
System.exit(0);
}
}

Of course, that doesn't help if the app is already running, but then anyway the
only way to stop it is to kill it.

Cheers,
Simon
 
T

TheRhythmic

Hi Edward,

Thanks for your quick reply.

This' the scenario:
Program A is running forever. I wanna stop it. How do I do it?

If I write another java program, say program 2, which says
'System.exit(0)', this doesn't work. I think because when I run java
program 2 using java command line tool, it executes in a new JVM I
think.

Thanks.
 
E

EdwardH

Program A is running forever. I wanna stop it. How do I do it?

Oh. You want to kill program A from outside of itself.

So find the java process that's running it and kill it.

"ps aux" and then "kill <pid>"
 
M

Moiristo

EdwardH said:
Oh. You want to kill program A from outside of itself.

So find the java process that's running it and kill it.

"ps aux" and then "kill <pid>"

To do it more nicely, I'd say that you create a public method
A.shutDown(), which simply executes 'System.exit(0);' when
it is called by App B
 
M

Matt Humphrey

Moiristo said:
To do it more nicely, I'd say that you create a public method
A.shutDown(), which simply executes 'System.exit(0);' when
it is called by App B

App B cannot invoke a method in a different JVM. App A must provide some
communication protocol (socket, RMI, stream read, shared file / mem, signal
handler) in order to be controlled externally. If App A provides no such
mechanism, you must use an OS-dependent technique to kill the process.

If you can modify the application, a common approach is to add a mini server
that accepts a client connetion as an administrative interface. A tiny
client app can then be used to control the main application. Some
applications use clever command-line arguments to gives the appearance the
app is invoking itself, even though what's happening is a different
application is communicating with the main app.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
M

M.J. Dance

Hi Edward,

Thanks for your quick reply.

This' the scenario:
Program A is running forever. I wanna stop it. How do I do it?

If I write another java program, say program 2, which says
'System.exit(0)', this doesn't work. I think because when I run java
program 2 using java command line tool, it executes in a new JVM I
think.

That's right. Starting java twice results in two java processes. Stopping one
doesn't ( necessarily ;) ) stop the other. But you can stop it anyway.

If you have access to program-A's console, you can just press Control+C.

Otherwise...

1. If you run some flavor of Linux (Unix?), you do the following:

1.1. Find out what program-A's pid is:

ps -A | grep java
(where -A is a switch, not program-A's name)

or

ps x | grep java

1.2. and then

kill <pid>

If the process doesn't want to die, you turn the radical-gauge nine notches up
;-) and

kill -9 <pid>

See also: killall.

2. If, on the other hand, you use Windows, just use Task Manager (End Process,
End Process Tree).
 
J

John O'Conner

Hi everyone,

This' the issue am facing:

A java application is runs forever. I want to provide the users a
facility to shut it down whenever they want to. How to do this?


Your app needs a processing loop in which it checks for a SHUTDOWN flag.
In addition, the app should have a thread that listens for a shutdown
message that it receives via a socket or shared memory (a file maybe).
When the external message is received, the thread sets the SHUTDOWN
flag. THe processing loop will eventually see the flag, and then it will
have the oppty to shutdown gracefully.
 
T

TheRhythmic

Hi,

Thanks to everyone for your replies. One more method I thought of:
Isn't it possible to obtain a reference to A & then use that reference
object to stop it?

Thanks.
 
J

jsillitoe

Matt is exactly right on the money for this. I have written several
applications that behave the exact same way. If you need more
confirmation, this is exactly how Tomcat is managed also.
 
J

jsillitoe

No, or at least not that I know of. Normally when you start a java
program it maintains it's own JVM for execution. This provides a layer
of separation between instances of a program running, even if it is run
from the same code base.
 
L

lordy

Hi,

Thanks to everyone for your replies. One more method I thought of:
Isn't it possible to obtain a reference to A & then use that reference
object to stop it?

Thanks.

I just wrote something (may be a better way to do it, so here goes ...) ,
Every thread registers itself with the ShutdownManager at a certain
shutdown level..
If the thread is blocking on a socket then it registers that socket..

Then the thread goes about its busienss like this..

try
ShutdownManager.register(socket,1);
while(!ShutdownManager.shuttingDown(socket) && other stuff ) {
process input
}
ShutdownManager.unregister(socket);
} catch (Exception e) {
ShutdownManager.unregister(socket);
}

Then the ShutdownManger. will cycle through the shutdown levels calling
either Thread.interrupt(), Socket.close() etc..

Generally the server sockets are at level 1.
SessionSockets level 2.
Output threads etc. level 3.
Core processes (eg internal hsqldb) at highest level.

The ShutdownManager enters a sleep, loop waiting for a shutdownRequest
from the rest of the app... This sets a flag and interrupts the sleep
loop.

There's probably a nice class out there that does this properly!

Lordy
 
S

steve

System.exit(0);

I don't see what the problem should be.

it could be that the program is launching multiple threads, the main program
can exit , but sometimes the threads hold onto the instance of the jvm.

I do this with a boot loader, the bootloader hits an exit(0) and dies, but my
other threads continue the application.

you need to close the other threads, or anything else you have launched from
your app, before the exit will fully close the JVM


steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top