Question

I am calling a jar via perl with the following command.

my $command = "$java_home/bin/java my_jar.jar ARG1 ARG2 ARG3";
my $result = `$command 2>&1;

However my JAR also expects arguments via STDIN. I need to know how to pass those arguments. I have tried passing them like normal arguments, and that didn't work. I read on a forum that OPEN2 might work however after reading the documentation I couldn't figure out how to make it work.

Any ideas on how to make this work would be great.

Thanks ahead of time.

Was it helpful?

Solution

Since you need to send and receive data from the Java process, you need two-way communication. That's what IPC::Open2 is designed to do. This allows you to create a dedicated pipe that renders STDIN/STDOUT unnecessary:

use IPC::Open2;

my $pid = open2( \*from_jar, \*to_jar, $command )
            or die "Could not open 2-way pipe: $!";

print to_jar, "Here is input\n";  # Pass in data

my $result = <from_jar>;          # Retrieve results

Also consider IPC::Open3 to handle errors as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top