Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restarting my Java Application

Status
Not open for further replies.

Vicis

Programmer
Jan 29, 2004
10
US
Hi all,

I'm working on a Java application that essentially stays running throughout a while loop. I have added code that, when an invalid value is received, the program breaks out of the while loop. I want to get my Java application to launch another instance of itself when this happens, and then kill itself.

Using Runtime.getRuntime().exec(), I executed the command "java -jar myJar.jar" and, as hoped, the application does restart. So far so good.

However, when running the application the first time in the command prompt, I am able to see all of my logger output, but when I restart, the command prompt goes away and the application runs without the command prompt available for logger output. I've tried updating the command to say "cmd /k pushd %APP_HOME% && java -jar %APP_HOME% + "\\myJar.jar" where APP_HOME is an environment variable that maps to the path of my JAR and although I've been able to run this command from the Run menu of Windows XP without a problem (meaning the application runs), but when the command is executed upon a restart attempt, nothing seems to change.

Due to the nature of the business I work at, I cannot display the full source code. However, here is the code that attempts the restart. Note that startCommand at this point equals "java -jar myJar.jar" or "cmd /k pushd %APP_HOME% && java -jar %APP_HOME% + "\\myJar.jar" (I've tried both):

try
{
restartProcess =
Runtime.getRuntime().exec(startCommand);
}

catch (IOException e2)
{
logger.warn("Exception triggered.");
e2.printStackTrace();
}

System.exit(0);

Does anyone know why the console window doesn't get re-opened in this case?

Thanks in advance for the assistance.
 
What about writing a log with the invalid value and keep on running the same instance of the application?

Seems more logical to me.

Cheers,
Dian
 
Or a more OO approach: code your while method to throw an excepction

Code:
public static void doTheJob() {
   while(true) {
     // do whatever
     if (notValidInput)
        throw new InvalidInputException();
    }
} 
[code]

And call it from your main method

[code]

public void mainMethod() {
 while (true) {
   try {
     doTheJob();
   } catch (InvalidInputException IIEe) {
     processErrors();
   }
 }
}

Cheers,
Dian
 
Thank you very much for the response.

The reason I am asking about restarting the application is because there is a lot of data that needs to be cleaned up, and it was decided by the team I'm in that it'd be easier (or so we thought) to simply launch a new instance of the application. If necessary, I can see if having the application itself relaunching is enough without having the command prompt visible, but I wanted to see if this was something that could be handled in the code. I'm hoping maybe I'm just using the wrong command.

At first I thought maybe running System.exit was messing up the re-launch, but then remembered that the application itself wouldn't have re-launched if exit killed off the wrong Java process.
 
If you have all the data inside an object, it gets garbage collected after being dereferenced ...

Cheers,
Dian
 
Unfortunately, this wasn't the case, even when I called System.gc(). It doesn't surprise me, as this application is quite large. I had initially attempted to null out the main object that is created, and another main object that is created within the first one, but eventually I learned that the smaller components within were not getting cleared as I had expected.

To get back to my main question, is it basically impossible to launch another instance with the command window, or is my command wrong? It seems silly that the application itself re-launches without issue, but not the display for the logging output.

Thank you again for the assistance.
 
You can redirect your error output with >>x.log.
 
I insist: identify the memory leaks and get them out. Keep everything in the Java world. Using command line from Java to invoke a Java class is not a good solution.

Cheers,
Dian
 
I'm sorry, but as I mentioned, that is not an option with our current development.

I appreciate the help, but I think I'll try another forum and see if I can find some answers there.

Thank you for your time.
 
As said before, you really should not have to kill your java process running the previous/current application.
What you are doing is just a really poor workaround for what is clearly badly coded Java.

That said, its your choice, and we've all done dodgy hacks to get around problems in the past.

So, from reading your post, your problem appears to be the logging issue. The answer is simple - you are not closing your FileOutputStream/PrintWriter/other output stream to disk. When you decide to "kill" your application, you must make sure you close this file stream. While file handles are closed automatically by all OS's (ASFAIK Solaris, Linux, Win32) when a process exists (ie the java JVM running your app), it may be that you attempt to restart and write to a file handle with the new process before the old one can relinquish its file handle.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Have you tried with "start cmd /K ....." in your startCommand?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top