Can't run program with ProcessBuilder, runs fi

2019-03-02 09:51发布

On linux (debian), I can run this command:

/usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*"

I am trying to run it from a Java program instead with:

ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", "\".*\"");

System.out.println(pb.command()); prints the following, as expected:

[/usr/lib/jvm/jdk1.7.0_21/bin/java, -jar, ~/myjar.jar, ".*"]

However I don't get the same output from the program (it runs but the ouput looks as if the ".*" argument is not properly taken into account).

Any ideas why it doesn't work?

Note: the same code works fine on Windows.

1条回答
乱世女痞
2楼-- · 2019-03-02 10:25

Looks like the wildcard character is not being expanded using a glob. You can use a shell instead:

ProcessBuilder pb = 
       new ProcessBuilder("bash", "-c", "java -jar ~/myjar.jar \".*\"");

or you can remove the double-quotes around the wildcard:

ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", ".*");
查看更多
登录 后发表回答