views:

53

answers:

3

I'm making an editor-like program. If the user chooses File->Open in the main window I want to start a new copy of the editor process with the chosen filename as an argument. However, for that I need to know what command was used to start the first process:

java -jar myapp.jar blabalsomearguments // --- need this information
> Open File (fileUrl)
> exec("java -jar myapp.jar blabalsomearguments fileUrl");

I'm not looking for an in-process solution, I've already implemented that. I'd like to have the benefits that seperate processes bring.

A: 

If all else fails, try writing a batch/shell script to launch your app. In windows you can pass %CmdCmdLine% to Java to get the entire command line.

See http://www.robvanderwoude.com/parameters.php

Tahir Akhtar
A: 

As far as I know is there no portable way to get this info. I found a property in the gcj runtime but I doubt this will cover a large percentage of the users.

I think the accepted practice is "Try and Pray" :

Hope it is on the path, (the path IS available, so that can be checked)

if not, check if JAVA_HOME is defined, and use that to find java.

if not check in the most likely places on all OS's you have received bug reports for.

Well, it is messy... porbably best to check for JAVA_HOME and the path and ask the user to configure a JVL explicitely if that fails.

Peter Tillemans
+1  A: 

Since you are launching Java -> Java, you can use the existing classpath to set the classpath on the command line. This type of thing works really nice in the dev environment too.

ProcessBuilder selfLauncher = new ProcessBuilder(
 "java", "-cp", System.getProperty("java.class.path"),
 "com.my.mainClass" );
selfLauncher.start();

Update: For executable jar files, you will have a classpath which is simply the relative path to the jar file itself. If you want the command line arguments, you will have to save them from main, and re-apply them when launching.

You can see this by packing the following program into a jar. I'm not actually sure what happens if you have jars inside the executable jar file. They probably show up in the classpath.

public class TestJarPath {
    public static void main(String args[]) throws Exception {
        for (String s : args)
         System.out.print("[" + s + "] ");
        System.out.println();

        String cp = System.getProperty("java.class.path");
        for (String s : cp.split(";"))
          System.out.println(s);
    } 
}

For java -jar ..\tst.jar X, you get output like:

[X]
..\tst.jar
Justin
Also, use System.getProperty("java.home") to get the currently running JRE base dir (Sun JVM)
ddimitrov
Ah, yea, that's a nice trick too (if you have a different dev environment JVM)
Justin
I guess this'll work. What about VM arguments?
Bart van Heukelom
System.getProperties() will list any JVM properties, you should filter out the default stuff and only pass on custom properties (not on the list provided in the javadoc). Be carefull, for example if you specify a debug port, the subprocess will try to use the same port (and may fail).
Justin