-3

I need to call a perl script at a central location using java with arguements..I have used the following code and it does nothing..Can someone tell me whats missing

            final List<String> commands = new ArrayList<String>();                

            commands.add("perl");
            commands.add("I:/golden_scripts/make-tp-patch.pl");
            commands.add(destpath+"/"+"scrfile.txt.org");
            commands.add(pro_name);
            commands.add(patch);
            ProcessBuilder pb = new ProcessBuilder(commands);
            Process p1 = pb.start();
4
  • Impossible to answer without knowing all about your environment. Commented Nov 8, 2013 at 19:14
  • Is the quote in new ProcessBuilder("commands) a typo? Commented Nov 8, 2013 at 19:14
  • I am running it in eclipse EE inside a web applcation built using struts2 and java Commented Nov 8, 2013 at 19:15
  • stackoverflow.com/a/4048489/312407 Commented Nov 8, 2013 at 19:19

1 Answer 1

1

Assuming your command is correct (and not the typo with the extra " in your post) and your environment is set up correctly, you probably want to block until the perl script completes.

Process p1 = pb.start();

Just starts the program. you aren't waiting for it to complete. you probably want to call p1.waitFor() which will wait for the program to finish and return an exit code.

then when waitFor() returns you know the program is done or has errored out. It is probably also a good idea to use the inputStreams from getInputStream() and getErrorStream() to get the STDOUT and STDERR messages the perl script prints as it runs.

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

1 Comment

Failing to consume all the bytes from the Input and Error stream may cause the external program to lock up mid-execution. I'd say consuming those streams is a mandatory part of "execing" the external process correctly. I usually launch background threads to consume the streams and have the ProcessBuilder thread use waitFor right after launching the background threads.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.