简体   繁体   English

从Java调用Perl脚本(Eclipse中的JAX-WS)

[英]Calling Perl script from Java (JAX-WS in Eclipse)

I have a JAX-WS webservice that receives a string as parameter, calls a Perl script, and returns the string converted to upper case. 我有一个JAX-WS Web服务,它接收一个字符串作为参数,调用Perl脚本,并返回转换成大写形式的字符串。 It is running on Tomcat 8 (localhost on Eclipse). 它在Tomcat 8(Eclipse上的localhost)上运行。

When I type from the console: 从控制台输入时:

curl -X POST --data "mystring=HelloWorld" http://localhost:8080/MyServices/api/generatePath

Everything works except for the Perl call. 除Perl调用外,一切正常。 On the debugger I see that line is executed, but apparently nothing happens (not even errors). 在调试器上,我看到该行已执行,但显然没有任何反应(甚至没有错误)。 If I run perl /home/me/workspace/match.pl from the console it works perfectly. 如果我从控制台运行perl /home/me/workspace/match.pl ,则效果很好。 The path of the match.pl file is correct. match.pl文件的路径正确。

In addition, process.exitValue() return 2. 另外, process.exitValue()返回2。

@Path("/")
public class MyServices {
    @POST
    @Path("/generatePath")
    @Produces(MediaType.APPLICATION_JSON)

    public Response generatePathService(
            @FormParam("mystring") String myString) {

        Process process = null;

        try {
            process = Runtime.getRuntime().exec("perl /home/me/workspace/match.pl --lang en");
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        return Response.status(200).entity(myString.toUpperCase()).build();
    }
}

I got similar stuff some time ago. 我前段时间也有类似的东西。 The solution for me was to 'accept' the output of the program. 对我来说,解决方案是“接受”程序的输出。 Something like this: 像这样:

Process p = Runtime.getRuntime().exec(cmd);
final InputStream is = p.getInputStream();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while (is.available() != 0) {
                is.read();
            }
        } catch (IOException ex) {
        }
    }
}).start();

You should also try to get the error stream with p.getErrorStream() . 您还应该尝试使用p.getErrorStream()获取错误流。 An alternative could be to change your perl program in that way that there is nothing printed to standard out or error out. 一种替代方法是以这种方式更改您的perl程序,以使没有任何内容可以打印为标准输出或错误输出。

Or you call a shell script and redirect the output to dev/null (someting like ´blabla>/dev/null`. 或者,您调用Shell脚本并将输出重定向到dev / null(类似于blabla> / dev / null之类的东西。

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

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