2

I'm trying to run a perl scripts using this java code:

ProcessBuilder ps = new ProcessBuilder("perl","test.pl","test.txt","|", "test1.pl",">","result.txt");

So the first script reads a txt file and then print some result.

Then the second script get this output, does some others modifications, and then prints them in a new txt file result.txt

This command line works fine using cmd on windows, but when using java, there is a problem with the | and the >.

Is there a way to run a such command using java?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Yacino
  • 645
  • 2
  • 10
  • 32

1 Answers1

2

The problem is that the piping using ">" simply doesn't work (when you make those java "process" calls).

What you have to do instead: when creating such a process, you can (programmitically) acquire its input and output streams.

So instead of doing a piped command, you just do

perl test.pl

and your java code then has to read from stdout of that process and write that content into a file; or in your case, write it into the stdin of another process call that runs your second script.

See here on how to do that.

Alternatively, you could enhance your perl scripts to take a file name argument, so that you could call it like

perl test.pl --write-to tmp.txt
perl test1.pl --read-from tmp.txt --write-to test.txt

for example. Then your java side can make both calls in one shot.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248