4

I am trying to execute the following command from a java program: java -jar /opt/plasma/fr.inria.plasmalab.plasmalab-1.3.4.jar -t -a montecarlo -A"Total samples"=1000 -m models/translated/plasma/NaCl2.rml:rml --format csv -r models/translated/plasma/NaCl2.bltl:bltl

with the following code:

String totalSample  = "-A\"Total samples\"=1000";
String mcCommand = "java -jar " + MChecker.getAppPath() + " -t "
                + "-a " + "montecarlo " + totalSample
                + " -m " + mcModelRelPath + ":rml " + "--format " + "csv "
                + "-r " + customQueryRelPath + ":bltl";

Process process = Runtime.getRuntime().exec(mcCommand);
        int errCode = process.waitFor();
        //then get the output, and error

But it results in the following error: Wrong parameter description : Dynamic parameter expected a value of the form a=b but got:"Total

I ran the same command in a terminal and it worked without any problem. But when I create the command in Java and try to invoke the tool it does not work.

I think it's confused because of the totalSample parameter which includes a space. What I did next was to put "\ " space escape in paramater(String totalSample = "-A\"Total\\ samples\"=1000";), but it still refused to accept it. It gave the following error: Wrong parameter description : Dynamic parameter expected a value of the form a=b but got:"Total\

Then I run the same parameters with the ProcessBuilder object, like the following:

 String[] mcCommand = {"java", "-jar", MChecker.getAppPath(), "-t",
         "-a", "montecarlo",totalSample, "-m",
         mcModelRelPath + ":rml", "--format", "csv", "-r",
         customQueryRelPath + ":bltl" };

ProcessBuilder pb = new ProcessBuilder(mcCommand);
Process process = pb.start();
process.waitFor();

But it still did not work and threw some custom exceptions.

I am currently out of options -- do you have any idea why this command does not work with Java, when it works just fine from the terminal interface?

BTW: I ran the same code on Windows it worked perfectly, but I have to run this code on Ubuntu OS.

Many Thanks

Chad
  • 1,750
  • 2
  • 16
  • 32
Memin
  • 3,788
  • 30
  • 31
  • if you think it is the white space, have you tried calling trim() to eliminate it and make sure this isn't your issue? – GregH Jul 21 '15 at 16:39
  • Can you change the binary in the jar to print out its arguments, say, one per line? Or can you write a little jar which does so and invoke it with the same command line? Maybe that would give an insight into the issue. – Andy Turner Jul 21 '15 at 16:42
  • @peggy the space character should be there in the "Total samples", but when it is executed, the application takes Total part until space and ignores the rest. Somehow, I need to run the command as whole. – Memin Jul 21 '15 at 17:30

2 Answers2

0

It was weird that, Java Process and ProcessBuilder classes could not pass the parameters properly. I don't know why, but since I was able to execute the command from terminal. I decide to call the terminal first and then execute the command. Therefore, I changed my command as following

String mcCommand[] = {
            "/bin/sh",
            "-c",
            "java -jar /opt/plasma/fr.inria.plasmalab.plasmalab-1.3.4.jar -t -a montecarlo "+totalSample+" -m models/translated/plasma/NaCl2.rml:rml --format csv -r models/translated/plasma/NaCl2.bltl:bltl" };

Despite it is platform dependant solution, currently it is ok for me.

Memin
  • 3,788
  • 30
  • 31
0

Don't add extra quotes for Total samples when using ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", MChecker.getAppPath(), "-t",
        "-a", "montecarlo", "-ATotal samples=1000",
        "-m", mcModelRelPath + ":rml", "--format", "csv",
        "-r", customQueryRelPath + ":bltl");
//...
Roman
  • 6,486
  • 2
  • 23
  • 41
  • Have you tried it? When you execute command `xxx -A"y z"=1` in the terminal in linux/unix, the quotes are removed by the shell, they are there only to preserve whitespace-containing argument, so it will run program `xxx` with one argument: `-Ay z=1`. It is actually equivalent to `xxx "-Ay z=1"` or `xxx -Ay\ z=1`. – Roman Jul 21 '15 at 23:34
  • Yes I tried without quotes, it does not work.. It seems the quotes are required by the external tool. When I invoke the tool through shell with the quotes the command works ( I have answered below), but if remove the quotes it does not even with extra shell parameter. But for now, through shell with quotes works for me. thank you. – Memin Jul 21 '15 at 23:42
  • It doesn't make sense. If I'm not demanding too much, could you show the error that you get when using my code? – Roman Jul 21 '15 at 23:56