in reply to Calling Perl with arguments From Java

Hi rgwest61,

I'm not sure I fully understand your description. So you want to write a Perl-only version of a Java tool you have? (In that case, DBD::Oracle is probably helpful.) And you then want to call this Perl tool from Java?

I believe the standard method for running an external process from Java is java.lang.ProcessBuilder.

Earlier you wrote about Inline::Java, so if that's what you're talking about, then perhaps Inline::Java::Callback is what you're looking for (calling Perl from Java code within Inline::Java).

Hope this helps,
-- Hauke D

  • Comment on Re: Calling Perl with arguments From Java

Replies are listed 'Best First'.
Re^2: Calling Perl with arguments From Java
by rgwest61 (Initiate) on May 31, 2016 at 20:18 UTC

    Hauke D I have tried the following two ways of calling a simple Perl script that currently just prints out the fact that it has been reached from a Java Application. Both attempts were found from an internet search and have not been successful - the expected output is not observed in the Linux window that starts the Java application. Just a little background - the Java code and simple Perl script were developed using the Eclipse IDE and the Perl script resides within one of the previously existing Java packages (Utils)

    // First Method List<String> cmds = new ArrayList<String>(); cmds.add("perl"); cmds.add("/home/rwest/workspace/src/AppMaint/Utils/PerlUtil.pl"); ProcessBuilder pb = new ProcessBuilder(cmds); pb.start(); // Second method Process p = RunTime.getRuntime().exec("perl /home/rwest/workspace/src/ +AppMaint/Utils/PerlUtil.pl"); p.waitFor();

    Other things I have done in an attempt to get either method to work: 1) Changed the group (chgrp) and access rights (chmod) so that the Perl script exists in the same group as the Java application and has read and execute permission. 2)Added /home/rwest/workspace/src/AppMaint/Utils/PerlUtil.pl to the Eclipse manifest file classpath line. 3) Executed the Perl script from Eclipse IDE with success. 4) Stepped through Eclipse debugger to make sure the applicable Java application lines of code to execute the Perl script are executed.

      Hi rgwest61,

      the expected output is not observed in the Linux window that starts the Java application

      That's normal; I suggest you study the documentation of ProcessBuilder:

      By default, the subprocess writes standard output and standard error to pipes.

      You'll either have to read these pipes yourself, which is the normal approach if your Perl script is generating output that your Java process is interested in, or you can have them inherit the Java process's streams by calling the methods such as pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); for each stream you want to inherit, or simply pb.inheritIO(); to inherit all three stdin/out/err streams. You should also inspect the Process's exitValue() to see if it's zero (normal exit) or not.

      Hope this helps,
      -- Hauke D