how to restart java application, remembering its command line arguments

后端 未结 5 373
一整个雨季
一整个雨季 2021-01-14 03:07

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

5条回答
  •  一整个雨季
    2021-01-14 03:59

    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, ...

提交回复
热议问题