简体   繁体   English

无法从Java代码调用Perl脚本

[英]Unable to call Perl script from Java code

I am using JAX-WS in Eclipse (tomcat8 localhost) and I need to call a Perl script from it. 我在Eclipse中使用JAX-WS(tomcat8本地主机),并且需要从中调用Perl脚本。 Using the code below, I am able to reach the first three lines of Match.pl. 使用下面的代码,我可以到达Match.pl的前三行。 However, when it reaches use Stats; 但是,当达到use Stats;时,请use Stats; , the process returns exitvalue 2. ,该过程返回exitvalue 2。

The Stats.pm file is in the same directory as Match.pl, which has been exported to PERL5LIB with hello.sh script. Stats.pm文件与Match.pl位于同一目录中,该文件已使用hello.sh脚本导出到PERL5LIB。

What am I doing wrong? 我究竟做错了什么? Is there a way to use relative paths instead of absolute ones? 有没有办法使用相对路径而不是绝对路径?

@Path("/")
public class MyService {
    @POST
    @Path("/generatePath")
    @Produces(MediaType.TEXT_PLAIN)
    public Response generatePathService(
            @FormParam("url") String fileURL) throws IOException, InterruptedException {
        Process p0 = Runtime.getRuntime().exec("sh /home/me/workspace/MyService/hello.sh");
        p0.waitFor();
        Process p = Runtime.getRuntime().exec("perl /home/me/workspace/MyService/Match.pl");
        p.waitFor();

        return Response.status(200).entity(fileURL).build();
    }
}

Match.pl Match.pl

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Stats;

Stats.pm Stats.pm

#!/usr/bin/perl

package Stats;

use strict;
use warnings;

hello.sh 你好

#!/bin/bash

export PERL5LIB=/home/me/workspace/MyService

Try adding: 尝试添加:

use FindBin;
use lib $FindBin::RealBin;

This will add the directory that the script is in to the library search path. 这会将脚本所在的目录添加到库搜索路径。

Edit: The shell script is not working because it changes the environment of the shell process, not the Java process that spawned it. 编辑:Shell脚本不起作用,因为它更改了Shell进程的环境,而不是产生它的Java进程的环境。

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

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