0

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?

amon
  • 57,091
  • 2
  • 89
  • 149
user2254173
  • 113
  • 1
  • 2
  • 9

2 Answers2

2

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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

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);
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38