Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2826
  • Last Modified:

Restarting a java program

I have a fairly complex Java program that I need to be able to restart. So far, I have done that by having a small external program ("launcher") that calls the main program, so that when I want to restart, I can kill the main program thread, and have the launcher relaunch it.

The problem with the launcher is that it is a pain in the butt to install and maintain, and it's so poorly written that it's barely worth it to do either.

My question is, can I get a program to re-launch itself, and how?. I don't mean just dispose of the JFrame and recreate, I'm talking about complete restart. I don't need to restart the JVM ( I don't think) but I do want the program to start again at main().

One approach I thought of is creating a thread that will kill all the other threads in the program, then relaunch the event-dispatch thread and die itself. Is this possible? Is there a simpler way?
Thanks
0
bored_shiva
Asked:
bored_shiva
  • 8
  • 4
  • 3
2 Solutions
 
CEHJCommented:
You just need to maintain the main functionality in another thread - outside the main app thread
0
 
objectsCommented:
only really if the application is written in sucha way that it is possible.

restarting the vm is a lot safer, think u would be better off focussing on simplifying doing that.
Can't you just write a script to do, so it would be a single command. You could schedule for it to be done automacally on a regular basis if that met your needs
0
 
objectsCommented:
> One approach I thought of is creating a thread that will kill all the other threads in the
> program, then relaunch the event-dispatch thread and die itself. Is this possible? Is there a simpler way?

wouldn't be enough. you would need to also unload all the classes.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bored_shivaAuthor Commented:
CEHJ,
What do you mean by that? It's very easy to write a  thread class that calls the main app thread, but how do I go about killing the old thread first?

Objects,
This program runs on multiple platforms, so a script it not a good solution for me, the restart has to come from within the Java code. Especially given that that I want the user to be able to restart on demand, rather than regular restart.
I'm fine with restarting the JVM if that's what it takes, the question then becomes how do I make it start again, and how do I make sure it gets the same parameters as before? Above all, is there a simple way to implement this from inside the program?
 
0
 
objectsCommented:
Use a launcher thread, which restarts it then. I'd suggest using Runtime.exec() to start a new vm, far safer option. Without restarting the vm I would suggest lots of testing first cause there is a bit of risk involved.
The launcher could maintain all the settings required.

0
 
objectsCommented:
you could still use a script option

eg. have a startup script that did something like this

if already running
   kill it
end
do
   java MyApp
while restart marker exists

0
 
bored_shivaAuthor Commented:
Runtime.exec() is problematic, since we're using several different platforms, so native commands could be hard to maintain.
0
 
CEHJCommented:
>>
CEHJ,
What do you mean by that? It's very easy to write a  thread class that calls the main app thread, but how do I go about killing the old thread first?
>>

You can't *always* interrupt() a thread, but you could try
0
 
objectsCommented:
> Runtime.exec() is problematic, since we're using several different platforms, so native commands could be hard to maintain.

try the startup script idea I posted then.
you'd just have a different script for each platform, not much overhead there.

0
 
bored_shivaAuthor Commented:
CEHJ,

Could I write an external "monitor" thread such that it would be in one jar, and the program will be in another jar, and the monitor would be able to pool a flag from the program, and restart it? I don't thin I have to unload the classes, just reset the state.
0
 
objectsCommented:
its not really an issue with the launcher/monitor. Thats the easy part, its your application that you need to ensure is restartable/reloadable. See my earlier comments, you're app may be in a worse state than before the restart if its not setup to be restartable.


0
 
CEHJCommented:
Absolutely. The two classes don't need to be coupled and shouldn't be. Just make the one being watched Runnable and place its main functionality in the run method. Does it actually return any kind of result?
0
 
objectsCommented:
> hread such that it would be in one jar, and the program will be in another jar

what jar it is in doesn't make any difference.

restarting the vm is the only safe option that I can see.

0
 
objectsCommented:
the multiple platforms shouldn't really be an issue either as you'd have a startup script on each platform already wouldn't you. They each really need one.

0
 
bored_shivaAuthor Commented:
Both CEHJ and objects gave me a lot to think about. I'm going to have to play around with the code and see which method works best.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now