【问题标题】:Unable to call Perl script from Java code无法从 Java 代码调用 Perl 脚本
【发布时间】:2015-06-21 23:37:46
【问题描述】:

我在 Eclipse (tomcat8 localhost) 中使用 JAX-WS,我需要从中调用 Perl 脚本。使用下面的代码,我可以到达 Match.pl 的前三行。但是,当它到达use Stats; 时,进程返回退出值 2。

Stats.pm 文件与 Match.pl 位于同一目录中,该文件已使用 hello.sh 脚本导出到 PERL5LIB。

我做错了什么?有没有办法使用相对路径而不是绝对路径?

@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

#!/usr/bin/perl

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

Stats.pm

#!/usr/bin/perl

package Stats;

use strict;
use warnings;

hello.sh

#!/bin/bash

export PERL5LIB=/home/me/workspace/MyService

【问题讨论】:

  • 你的路径中有 perl 吗?有什么例外吗?
  • 是的,perl 在路径中(部分为 Match.pl 运行)。我在 Eclipse 中看不到任何异常
  • 我的猜测是在perl脚本运行之前没有完成导出
  • export 在同一个 shell 中工作,并且由于您正在启动两个不同的进程,所以我认为第二个进程无法使用 export。尝试结合这两个脚本并检查结果。
  • 从 hello.sh 调用 Match.pl

标签: java eclipse perl path


【解决方案1】:

尝试添加:

use FindBin;
use lib $FindBin::RealBin;

这会将脚本所在的目录添加到库搜索路径中。

编辑:shell 脚本不起作用,因为它改变了 shell 进程的环境,而不是生成它的 Java 进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多