2

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();
    }
}
Julio Diaz
  • 9,067
  • 19
  • 55
  • 70
  • [no idea from this question, maybe ordering of parameters, maybe exec(new String[] {"...."})](http://stackoverflow.com/a/6164084/714968) – mKorbel Mar 02 '13 at 20:15
  • 1
    You don't appear to be handling the InputStream, OutputStream and error InputStream. The Error Stream in particular may show you important error messages. Also if you don't handle these Streams, you may fill up OS buffers rendering your process non-functioning. – Hovercraft Full Of Eels Mar 02 '13 at 20:15
  • I will update the code to handle this – Julio Diaz Mar 02 '13 at 20:18
  • 4
    I could be wrong, but I'm pretty sure you don't need to "quote" argument groups like ` "\"measure 3 4;measure1 2\""`, ` "measure 3 4;measure1 2"` should be just fine – MadProgrammer Mar 02 '13 at 20:38
  • @MadProgrammer that was it. how can i accept your answer? – Julio Diaz Mar 02 '13 at 20:44
  • @JulioDiaz I added an expanded answer, double check it and see if it sounds right – MadProgrammer Mar 02 '13 at 20:53

1 Answers1

5

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"};
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366