简体   繁体   English

如何从我的 Java 程序执行 Perl 脚本?

[英]How can I execute a Perl script from my Java program?

I am writing a little tool that needs to invoke a Perl script to do some stuff.我正在编写一个需要调用 Perl 脚本来执行某些操作的小工具。

I already know and written the code on how to call it, but I have to provide the full-qualified path to the Perl script for the exec() .我已经知道并编写了有关如何调用它的代码,但我必须为exec()提供 Perl 脚本的完全限定路径。

Is there any possibility to provide that script as a sort of package that can be invoked directly?是否有可能将该脚本作为一种可以直接调用的 package 提供?

I want to generate an executable JAR later and want to also have the whole tool to provide the Perl script and not to know where it is located.我想稍后生成一个可执行文件 JAR 并且还想拥有提供 Perl 脚本的整个工具并且不知道它位于何处。

Currently its like:目前是这样的:

private void extractAudioStream() {
    inputFile = "C:\\Users\\Stefan\\Documents\\temp_tshark\\forward_stream.pcap";
    outputFile_audiostream = "C:\\Users\\Stefan\\Documents\\temp_tshark\\forward_stream.pcm";

    String perl_cmd = "perl C:\\Users\\Stefan\\Documents\\tshark_bin\\rtp_dump.pl " + inputFile + " " + outputFile_audiostream;

    try {
        p = Runtime.getRuntime().exec(perl_cmd);

Hope question is clear enough.希望问题足够清楚。

I think this may not be as easy because the system perl command awaits the full-qualified path?我认为这可能不那么容易,因为系统perl命令等待完全限定路径? Is there then any possibility to detect my current location and pass it to my cmd-String?那么是否有可能检测到我当前的位置并将其传递给我的 cmd-String?

String perl_cmd = "perl -e \"print \\\"bla\\n\\\";\";"
Runtime.getRuntime().exec(perl_cmd);

Edit: I forgot to put a space between -e and the script;编辑:我忘了在-e和脚本之间加一个空格; you also have to escape backslashes and double-quotes in your script (twice if you're the script inline in Java instead of reading it from a jar file), which makes things a little awkward for more than one-liners.您还必须在脚本中转义反斜杠和双引号(如果您是 Java 中的内联脚本,而不是从 jar 文件中读取它,则转义两次),这使得不止一行的事情有点尴尬。

So it would probably be better to use Process.getOutputStream to write the script to the Perl interpreter's standard input:因此,最好使用Process.getOutputStream将脚本写入 Perl 解释器的标准输入:

  String perlScript = "print \"hello\";"; // get from a jar with Class.getResourceAsStream()
  Process p = new ProcessBuilder("perl").redirectErrorStream(true).redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
  BufferedWriter w = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
  w.write(perlScript);
  w.close();
  p.waitFor();

if you read/store your perl script in a string, you can execute it like this:如果您在字符串中读取/存储 perl 脚本,则可以像这样执行它:

perlScript = "print \"HELLO\\n\"; print \"CIAOOOO\\n\";";
String perl_cmd = "echo \"" + perlScript + "\" |perl";
Runtime.getRuntime().exec(perl_cmd);

Hope it helps...希望能帮助到你...

You can write the script out to a file, say a temporary file, and execute it from there, then delete it when you're done.您可以将脚本写到一个文件中,比如说一个临时文件,然后从那里执行它,然后在完成后将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM