我应该如何在 Java 中调用 Perl 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/603554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:45:25  来源:igfitidea点击:

How should I call a Perl Script in Java?

javaperl

提问by Fernando Briano

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

我阅读Runtime.getRuntime().exec("perl script.pl")是一种选择,但这是最好的方法吗?

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.

无论如何,是exec()从 Java 调用 Perl 脚本的好方法吗?我应该注意,我正在开发一个 Java Web 应用程序,所以这里的安全性是一个问题。

采纳答案by luiscubal

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.

您可以使用或使用Process API。Process API 允许您获取脚本的输出,因此您可以进行通信。Runtime.getRuntime().exec()

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

exitValue()并且getInputStream()似乎是您所需要的。

回答by John T

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

exec() 可能是最好的选择,您可以使用exitValue()捕获它的返回值。您可能还对Inline::Java感兴趣。

-John

-约翰

回答by Frakkle

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

这概述了如何相当优雅地完成它,尽管它可能比它的价值更多:http: //search.cpan.org/~nwclark/perl-5.8.9/jpl/docs/Tutorial.pod

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

概述:
得到了 JPL 的大力支持,但它是一个复杂的过程:

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

JPL 预处理器解析 .jpl 文件并为 Perl 方法生成 C 代码包装器。它还生成 Java 和 Perl 源文件。

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

C 编译器编译包装器并将其链接到 libPerlInterpreter.so 共享库,从而为包装器生成一个共享库。

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

Java 编译器编译 Java 源文件,该文件使用本机方法加载包装器。

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

包装器将 Java 代码连接到 Perl 源文件中的 Perl 代码。

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

幸运的是,通用的 Makefile.PL 简化了这个过程。这是一个为您生成 Makefile 的 Perl 脚本。

回答by kmonsoor

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'

请记住,无论 Perl 脚本创建什么文件,它都是在 Java 工作文件夹中创建的。只需将该文件称为“./myPerlCreatedFile.ext”