0

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.

1 Answer 1

3

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.

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

4 Comments

Thanks. I am trying this... however when I try to run the command using open2 it seems to time out. Any ideas what would cause this?
Nevermind... it seemed to be because I wasn't closing the Writer / Reader. I fixed that but still not working. Looking into it more.
@CameronJones : Is your Java process autoflushed? It could be buffering
Turns out what the jar was connecting to was shut off. However, this part worked great. Thank you.

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.