简体   繁体   English

无法从Java调用Perl脚本

[英]Not able to invoke Perl script from Java

I am trying to invoke Perl script from Java but seems like I am not able to do it. 我试图从Java调用Perl脚本,但似乎无法执行。

Here is my Perl script which creates a file. 这是我的Perl脚本,用于创建文件。 It is a simple script. 这是一个简单的脚本。

use strict;
use warnings;
open(my $fh, '>', 'report.txt');
print $fh "My first report generated by perl\n";
close $fh;
print "done\n";

Here is my Java code which is invoking above Perl script. 这是我在Perl脚本上方调用的Java代码。

package perlfromjava;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PerlFromJava {

public static void main(String[] args) {
    try {

        String command = "perl $HOME/Documents/hello.pl";
        System.out.println(command);
        Process proc = Runtime.getRuntime().exec(command);
    } catch (IOException ex) {
        Logger.getLogger(PerlFromJava.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

When I am running Perl scrip from command like, it is working perfectly but when I am invoking Perl script from Java, report.txt file is not getting created. 当我从类似的命令运行Perl脚本时,它工作正常,但是当我从Java调用Perl脚本时,未创建report.txt文件。

Why is it happening? 为什么会这样呢?

Thanks 谢谢

You can't use the $HOME variable from the Java Runtime . 您不能在Java Runtime使用$ HOME变量。 In Java, you could use System.getenv("HOME") or the cross-platform System.getProperty(String) to get it, like 在Java中,您可以使用System.getenv("HOME")或跨平台的System.getProperty(String)来获取它,例如

String command = "perl " + System.getProperty("user.home") 
        + "/Documents/hello.pl";

A list of available System Properties is included in The Java Tutorials. Java教程中包含可用的系统属性列表。

Edit 编辑

Don't forget to wait for the Process to complete, 不要忘记等待Process完成,

try {
    Process proc = Runtime.getRuntime().exec(command);
    proc.waitFor();
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

actually your code is working .but the problem is the file created by perl is generated in where you run java file.if you are using a ide then file has surely created inside of that project folder .if you search "report.txt" you will find the file.to understand change your perl script to 实际上您的代码在工作。但是问题是由perl创建的文件在您运行Java文件的位置生成。如果您使用的是ide,则肯定在该项目文件夹中创建了文件。如果您搜索“ report.txt”,将找到该文件。以了解将您的perl脚本更改为

use strict;
use warnings;
open(my $fh, '>', 'C:/Users/Madhawa.se/Desktop/js/report.txt');
print $fh "My first report generated by perl\n";
close $fh;
print "done\n";

intead give report.txt give full path where u like to create report.txt file in the perl script .and see it's working. intead Give report.txt提供您希望在perl脚本中创建report.txt文件的完整路径,并查看其工作情况。

 try {

    String command = "perl C:\\Users\\Madhawa.se\\Desktop\\js\\mm.pl";
    Process process = Runtime.getRuntime().exec(command);
    process.waitFor();
    if (process.exitValue() == 0) {
        System.out.println("Command Successful");
    } else {
        System.out.println("Command Failure");
    }
} catch (Exception e) {
    System.out.println("Exception: " + e.toString());
}

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

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