我如何管道输入到Perl的Java命令? [英] How can I pipe input into a Java command from Perl?

查看:106
本文介绍了我如何管道输入到Perl的Java命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Java程序运行一个字符串,然后检索输出。 Java程序通过标准输入接受字符串。以下工作:

I need to run a string through a Java program and then retrieve the output. The Java program accepts the string through standard input. The following works:

my $output = `echo $string | java -jar java_program.jar`;

有一个问题: $ string 只是什么。关于这个问题的一个很好的解决方案的任何想法?

There is one problem: $string could be just about anything. Any thoughts on a good solution to this problem?

推荐答案

我建议你看看 IPC :: Run3 模块。它使用非常简单的接口,允许获取 STDERR STDOUT 。这里是小例子:

I suggest you to look at IPC::Run3 module. It uses very simple interface and allow to get STDERR and STDOUT. Here is small example:

use IPC::Run3;
## store command output here
my ($cmd_out, $cmd_err);
my $cmd_input = "put your input string here";
run3([ 'java', '-jar', 'java_program.jar'], \$cmd_input, \$cmd_out, \$cmd_err);
print "command output [$cmd_out] error [$cmd_err]\n";

请参阅 IPC :: Run3 与其他模块比较

这篇关于我如何管道输入到Perl的Java命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆