how to restart java application, remembering its command line arguments

本秂侑毒 提交于 2019-12-01 05:34:02

问题


I have a java application. It can be started with couple of command line flags. I want to provide ability "restart" the application by user.

Currently we save the the arguments on a control file, reads it when restarting the application. What is the best way to restart the application - how can I retain the command line arguments?


回答1:


Using the RuntimeMXBean you could retrieve , Classpath, Bootclasspath etc.

package com;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

class JMXTest {
    public static void main(String args[]) {
        try {
            for ( int i = 0 ; i < args.length ; i++ ) 
                 System.out.println( "args   :" + args[i] );

            RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
            System.out.println( "boot  CP:" + mx.getBootClassPath() );
            System.out.println( "      CP:" + mx.getClassPath() );
            System.out.println( "cmd args:" + mx.getInputArguments() );
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
}



回答2:


invoke new using

java -jar appname.jar arg1 arg2  

close current one using

System.exit(0);   

Here you won't face problem of retaining arg

Here is example to invoke commands from java app




回答3:


Anyway, you will have to persist the commandline arguments. If the set of arguments is pretty fixed, consider writing a small batch or shell script file that does nothing but calling java with this set of arguments.

If you just want to start it once with arguments and then, if you restart the application without arguments, want to have it to use the arguments from the previous call, do something like that:

public static void main(String[] args) {

   if (args.length == 0)
     args = readArgsFromFile();
   else
     writeArgsToFile();

   // ...

}

Sidenote: For simplicity reasons I've reused args. For better code, if needed, copy the received or stored parameters to another data structure, another array, a Properties instance, ...




回答4:


It varies according to an OS of the user, If you really want to do it OS cross-platform compatible. Then you should supplied starting scripts : shell for linux like OS / bat for windows, these scripts set up the classpath and arguments.

I don't think that creating "restart" button in the application is a wise decision, but If you want something like "eclipse restart", you should take a look at RuntimeMXBean which can get booting classpath for you.




回答5:


Why not serialize and create the object again from disk when restarted?

You will need to implement the Serializable interface in a "CommandLineParams" class to do this.

I think it's the most structured way to accomplish what you are trying to do.



来源:https://stackoverflow.com/questions/3854997/how-to-restart-java-application-remembering-its-command-line-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!