Can I use main() to restart my application?
Asked Answered
C

7

6

I am researching a way to restart my java application by clicking a button on the GUI. I searched the web and came across main( new String[0]). I need to understand if this is an valid way to restart my application. Can someone please advise thanks.

private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {

    main( new String[0]);
    }

Edit Would this be better?

private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {
    classname.this.dispose();
    main( new String[0]);
    }
Chaffinch answered 31/10, 2012 at 23:38 Comment(1)
If your app's GUI is still up, then the app is already runningFoochow
C
13

It calls the static main method with an empty string array. See if this makes it any clearer:

String[] args = new String[0]; // Or String[] args = {};
main(args);

Admittedly it's unusual to call a main method from non-main method... and this won't really "restart" the application. It will call it from within your existing handler which may well have nasty consequences. I wouldn't recommend it.

If you can work out a way to start an entirely clean process, that would be a far more reliable "restart".

Calliecalligraphy answered 31/10, 2012 at 23:40 Comment(0)
M
7

You're not going to be able to restart your application without exiting the JVM - the JVM will have allocated objects, threads etc. and without a lot of housekeeping you're not going to easily trash this.

I think an easier way is to wrap your application in a script, and to get the script to restart your app if it exits with a particular exit code. That way you can trash your JVM completely via a System.exit() call, and if the script only restarts your app if it sees a particular exit code, you have the option of quitting, or quitting and restarting.

e.g. check out the JavaServiceWrapper. This provides a capability to start a Java application with particular config/parameters, and to control restart behaviour. Note that it provides a particular API call to invoke a restart from within your application.

Mask answered 31/10, 2012 at 23:45 Comment(0)
H
3

This is not a good way to restart your application, because the initial invocation of your application would still be running. If you "restart" your application like that enough times, you would get a stack overflow.

If you must restart your application without quitting it (which is unusual), you can set up a try/catch block inside your main, put a loop around it, and continue the loop when you get an exception. Admittedly, this is more of a workaround than anything else, because it uses exceptions to control the "normal" program flow.

Hazen answered 31/10, 2012 at 23:41 Comment(3)
Please never restart an application like so. The correct way to restart an application is to execute a new Java command with your program's necessary classpath/options.Malpighiaceous
That is some ugly way to restart.Manuelmanuela
@Vulcan Admittedly, this is a solution of last resort, bordering on the hack. I edited my answer to make it clear.Hazen
M
1

As you read that.

private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {

    main( new String[0]);
    }

What it is doing is on bnnewsaleActionPerformedit is calling main() with an empty new string.
This wont restart your application.

To restart means, stop and start again.

Whereas with your code, it will start your application again inside ypur current application.
Kind of like nesting it and it will have nasty effects for example : not resetting any static allocations.

Manuelmanuela answered 31/10, 2012 at 23:49 Comment(0)
S
1

Here is a concrete example of what @Brian Agnew already suggested above.

In you Java code, exit with a a particular exit for in case of restart

if(restart){

System.exit(5);

}else{
 System.exit(0);
}

Now create a script which is actually used to kick start your application. I am giving example of windows batch script over here but you can worked similar scripts for other OS.

The batch File:

@echo off

:start

java Test %1
set exitcode=%ERRORLEVEL%

if %exitcode% == "5" (goto :start)

When you exit with the status code of 5 (it can be any integer) then you batch will restart the program (Test is the example class which has main method)

Saida answered 1/11, 2012 at 7:34 Comment(0)
J
0

main() is a function you defined in that class.
It takes a single argument of type string[].

This line calls that function, just like any other function.
It passes an empty string array (new String[0])

Since your main() function begins your application, this will "restart" it.

However, it will not reset any static state.

Jara answered 31/10, 2012 at 23:40 Comment(0)
B
0

There is nothing special about the main method in java, so calling main does not differ from calling any other method. Every bit of jvm initialization happens before the java runtime searches for the method named "main".

If you want to restart your application without exiting the jvm you have to avoid or clean up every bit of static state. For simple cases you can just write a class to manage your application lifetime and create a new instance of it for a restart.

class MyApplication{
    public void start(){}//setup all application state and run it
    public void shutdown(){}//close all Windows/Connections and Threads
}

For non trivial examples this can get ugly ThreadLocals/static variables/files marked delete on exit have to be taken care of. Also updates to application classes will only be visible with some classloader trickery.

Bedeck answered 1/11, 2012 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.