I have a ProcessBuilder that executes a shell script, it worked fine until I wanted to add an argument to the shell script from a variable. It should execute like this: ./test.sh testarg - I am although getting a Null Pointer Exception:
Exception in thread "main" java.lang.NullPointerException
at testOne.main(testOne.java:10)
Line 9:
String myarg = "testarg";
Line 10 (the one that gives me the error):
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c",
testOne.class.getResource("/test.sh " + myarg).getPath());
Had it like this before and it was working:
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c",
testOne.class.getResource("/test.sh").getPath());
I understand that I have have concatenated the /test.sh with the String and that it would be treated as filename now which is why it gives me the error. I can see the difference between between "/test.sh"
and "/test.sh" + myarg
and that it result in a invalid resource name. I do however not know how I could make it work so it executes test.sh with one argument that I set with the String.
Have you tried:
URL url = testOne.class.getResource("/test.sh");
if (url == null) {
throw new Exception("Cannot find /test.sh");
}
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c",
url.getPath() + " " + myarg);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.