Reputation: 113
I have to call a Perl script from a Java class. I am able to do that using
final ProcessBuilder builder = new ProcessBuilder("perl", "/home/abc.pl");
I am wondering if I can pass parameters something like
new ProcessBuilder("perl", "/home/abc.pl x y");
But it's throwing an error.
Could someone please suggest how this could be done?
Upvotes: 0
Views: 2275
Reputation: 3794
Use following code to execute your perl code from java.
final List<String> commands = new ArrayList<String>();
commands.add("perl");
commands.add("/home/abc.pl");
commands.add("x");
commands.add("y");
ProcessBuilder pb = new ProcessBuilder(commands);
Upvotes: 2
Reputation: 944568
From the documentation:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Each argument to the program you are calling needs to be a separate argument to the ProcessBuilder constructor.
new ProcessBuilder("perl", "/home/abc.pl", "x", "y");
Otherwise you are calling the equivalent of perl "/home/abc.pl x y"
and it will be unable to find a file called "/home/abc.pl x y" (since x and y are different arguments, not part of the file name).
Upvotes: 2