1

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.

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());

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 am however clueless how I could solve it.

Lee-B
  • 15
  • 3
  • 1
    Please post the entire exception stacktrace message. It is *very* important and often will tell you *exactly* what is wrong. – Hovercraft Full Of Eels Jan 31 '18 at 22:34
  • 1
    I addition to the stacktrace as Hovercraft Full Of Eels suggest, also show a [mcve]. – Code-Apprentice Jan 31 '18 at 22:36
  • 2
    This `testOne.class.getResource("/test.sh " + myarg).getPath()` does look fishy. – Hovercraft Full Of Eels Jan 31 '18 at 22:36
  • Added the entire exception stacktrace message. – Lee-B Jan 31 '18 at 22:37
  • What is line 10? – Code-Apprentice Jan 31 '18 at 22:37
  • @HovercraftFullOfEels it works very well without the + myarg – Lee-B Jan 31 '18 at 22:38
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Alex Shesterov Jan 31 '18 at 22:40
  • This question looks like a dupe of this one: https://stackoverflow.com/questions/31404420/java-open-file-from-classpath-with-a-space-character-in-it?lq=1 (which itself has no own answer post, but links to another dupe). – Tom Jan 31 '18 at 22:54
  • @Tom it's not the same problem, I have checked. – Lee-B Jan 31 '18 at 23:06
  • *"it works very well without the + myarg"* - So get rid of the "+ myarg"!! What you are doing in the working version is reading a resource containing a shell script. When you add arguments, it fails because the "path" no longer matches any resource. The `getResource(...)` returns `null` and you get an NPE because you don't check for `null`. That line of code (as written) does not make sense. – Stephen C Jan 31 '18 at 23:43
  • 1
    Basically, you need to **understand** how "bash -c" works / what it does. And figure out how to map that to what you are trying to do. (The latter is not clear from the question ....) – Stephen C Jan 31 '18 at 23:46

1 Answers1

1
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c",
        testOne.class.getResource("/test.sh " + myarg).getPath());

This attempts to do too many thing in a single line. You should break it up into several pieces, assigning intermediate results to variables so that you can more easily debug it:

final URL bashScriptResource = testOne.class.getResource("/test.sh " + myarg);
final String bashScriptPath = bashScriptResource.getPath()
final ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c",
        bashScriptPath);

Now use a debugger or add System.out.println() calls to print the values of each variable. This will show you exactly where the problem is.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    Thank you for your help, but in theory, nothing is wrong with the way I am doing it? – Lee-B Jan 31 '18 at 22:43
  • 1
    Since `new ProcessBuilder("/bin/bash", "-c", testOne.class.getResource("/test.sh").getPath());` works fine. – Lee-B Jan 31 '18 at 22:43
  • @Lee-B The NullPointerException shows that there is something wrong with the way you are doing it. Take the idea that I show here a step further at look at the difference between `"/test.sh"` and `"/test.sh" + myarg`. Which of these result in a valid resource name? – Code-Apprentice Jan 31 '18 at 22:54
  • I don't get what you mean, might me that is tired (so sorry, I really appreciate your help). All I want to do is to call a shell script with an argument. If I skip `+ myarg` it works. I do not know why adding `+ myarg` which is a String causes an error in Java. I tried to execute it in the shell and it works. `sh test.sh testargument` – Lee-B Jan 31 '18 at 23:11
  • @Lee-B Be careful separating what you want from what the code actually does. Look at this part: `testOne.class.getResource("/test.sh")` vs `testOne.class.getResource("/test.sh " + myarg)`. Neither of these execute a shell script. Instead they locate a resource, as the name suggests. The first tries to find a resource named `"/test.sh"`. The second tries to find a resource named `"/test.sh testarg"`. **A resource with this name does not exist.** Now when you attempt to execute the command with `ProcessBuilder`, you get an error because you are attempting to use a resource which doesn't exist. – Code-Apprentice Feb 01 '18 at 16:05