4

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?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Jayan
  • 18,003
  • 15
  • 89
  • 143

6 Answers6

2

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();
        }
    }
}
stacker
  • 68,052
  • 28
  • 140
  • 210
  • Thanks. RuntimeMXBean.getInputArguments() - does not fetch command line arguments passed to main method. I prints the properties etc passed to the command line. – Jayan Oct 04 '10 at 12:28
  • @StackTrace I added two lines to complete, I thought it is already clear how to get them. Classpath and VM args are not so easy to get. – stacker Oct 04 '10 at 12:53
  • Tested with java com.JMXTest -arg1 -arg2=val2 mx.getInputArguments() : This returns empty list. The javadoc says -- * Returns the input arguments passed to the Java virtual machine * which does not include the arguments to the main method. – Jayan Oct 05 '10 at 06:29
  • for ( int i = 0 ; i < args.length ; i++ ) System.out.println( "args :" + args[i] ); // returns the arguments to the main method. – stacker Oct 05 '10 at 07:02
1

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

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
jmj
  • 237,923
  • 42
  • 401
  • 438
1

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

Patrick
  • 1,717
  • 7
  • 21
  • 28
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

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.

lisak
  • 21,611
  • 40
  • 152
  • 243
0

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.

Jas
  • 1,141
  • 5
  • 16
0

Write here solution for closed How to restart the application after an error? My opinion - no duplication with this topic, it's a problem of recover working application.

If you can ran the Bash script instead of your program:

#!/bin/bash
while : ; do
    myprogram
    [[ $? -ne 0 ]] || break
done

And run as nohup script.sh &
See https://superuser.com/a/1223132/561025 and https://superuser.com/a/1223132/561025

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197