How should I call a Perl Script in Java?
Asked Answered
O

4

8

I read Runtime.getRuntime().exec("perl script.pl") is an option, but is this the best way to do it?

I'll need an answer from that script, so I'll have to read the script's return in some cases, although I might read it from a text file on other cases.

Anyway, is exec() a good way of calling a Perl Script from Java? I should note, I'm working on a Java Web Application, so security is an issue here.

Offstage answered 2/3, 2009 at 19:3 Comment(1)
Similar question: https://mcmap.net/q/1323622/-include-perl-in-javaPanatella
H
11

You can use Runtime.getRuntime().exec() or use the Process API. The Process API allows you to get the output of the script, so you can have both communicate.

exitValue() and getInputStream() seems to be what you need.

Haggis answered 2/3, 2009 at 19:22 Comment(1)
Use the below code for when running Java code on a Windows machine: process = r.exec("cmd /c perl D:\\perlexamples\\hello.pl");Digitalin
W
4

This outlines how to do it fairly elegantly, though it may be more effort than it's worth: http://search.cpan.org/~nwclark/perl-5.8.9/jpl/docs/Tutorial.pod

Overview:
Well-supported by JPL, but it is a complicated process:

The JPL preprocessor parses the .jpl file and generates C code wrappers for Perl methods. It also generates Java and Perl source files.

The C compiler compiles the wrapper and links it to the libPerlInterpreter.so shared library, producing a shared library for the wrapper.

The Java compiler compiles the Java source file, which uses native methods to load the wrapper.

The wrapper connects the Java code to the Perl code in the Perl source file.

Fortunately, a generic Makefile.PL simplifies the process. This is a Perl script that generates a Makefile for you.

Wolfy answered 2/3, 2009 at 19:19 Comment(1)
JPL was removed from Perl 5.10. I wouldn't recommend adopting a dead API. Use Inline::Java instead.Goulder
A
3

exec() is likely the best option and you can catch it's return value with exitValue(). You might also be interested in Inline::Java.

-John

Asafetida answered 2/3, 2009 at 19:6 Comment(0)
M
1

keep in mind, whatever file the Perl script create, it is created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

Music answered 10/2, 2011 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.