Search code examples
javaprocessbuilder

ProcessBuilder works starting jar but does not acknolwledge jar's arguments


I am trying to run jmol's jar inside a running java program. This is how I run it in the command line and it runs fine.

$ java -jar Jmol.jar 1644_____.pdb -o -J "measure 3 4;measure 1 2"

I am using a ProcessBuilder and it calls the jar file and the first argument correctly but not the rest. What am I missing?

import java.io.IOException;

class test{
    public static void main(String [] ar) throws Exception{
        run();
    }

    public static void run() throws IOException, InterruptedException{
        String INPUTPDB = "1644_____.pdb";
        String args[] = {"java", "-jar", "Jmol.jar", INPUTPDB, "-o", "-J", "\"measure 3 4;measure1 2\""};
        ProcessBuilder pb = new ProcessBuilder(args);
        //Runtime.getRuntime().exec(args);
        Process p = pb.start();
        p.waitFor();
    }
}

Solution

  • As I understand it, each parameter you pass to ProcessBuilder will be passed to the process as a separate argument.

    That means that when the process does an equivalent of args[x], your \"measure 3 4;measure1 2\" parameter will look like "measure 3 4;measure1 2" to the process (including the quotes).

    Unless the command is expecting the quotes, there is no need to quote the parameters

    Instead, try something like

    String args[] = {"java", "-jar", "Jmol.jar", INPUTPDB, "-o", "-J", "measure 3 4;measure1 2"};