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?

2
  • What error is it throwing? Commented Apr 11, 2013 at 10:41
  • can not find the abc.pl x y. if i remove the args it works Commented Apr 11, 2013 at 10:48

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

great help Quentin.. Many thanks for the quick reply and your time.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.