[英]Perl script is not fully executing when called from Java Program
I am trying to execute my Perl script from Java program. 我正在尝试从Java程序执行我的Perl脚本。 I have already installed Perl in my computer. 我已经在计算机上安装了Perl。 Below is my sample Perl file ( test.pl
). 以下是我的示例Perl文件( test.pl
)。
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use Path::Tiny;
use autodie;
my $dir = path("E:/perl/scripts");
my $file = $dir->child("file.txt");
my $file_handle = $file->openw_utf8();
my @list=('a', 'list', 'of', 'lines');
push @list,<*.doc>;
push @list,<*.pdf>;
push @list,<*.jpg>;
push @list,<*.pl>;
print "Value of list = @list \n";
And this is my Java code to execute Perl script. 这是我执行Perl脚本的Java代码。
Process process;
try {
process = Runtime.getRuntime().exec("cmd /c perl E:\\perl\\PerlCallbyServlet\\test.pl");
//process = Runtime.getRuntime().exec("perl E:\\perl\\PerlCallbyServlet\\test.pl");
process.waitFor();
if (process.exitValue() == 0) {
System.out.println("Command Successful");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Command Failure");
}
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
}
Now first when I am trying to run this Java class I am getting below output. 现在首先,当我尝试运行此Java类时,我的输出低于输出。
Command Successful
Value of list = a list of lines
But when I am running this script from command prompt using command 但是当我使用命令从命令提示符运行此脚本时
perl test.pl
My output is as below: 我的输出如下:
Value of list = a list of lines one.doc test.pl photo.jpg
So I am not getting same output of a Perl script executing from Java program as from command prompt. 因此,我从Java程序执行的Perl脚本输出与从命令提示符得到的输出不同。
Your Perl script does not set the working directory. 您的Perl脚本未设置工作目录。 I make the assumption that you want to add the contents of the directory E:/perl/scripts
to @list
. 我假设您想将目录E:/perl/scripts
的内容添加到@list
。 Try to add to the Perl script: 尝试添加到Perl脚本中:
chdir('E:/perl/scripts');
Sorry, I do not use Windows, so I can't test if that is the correct path under Windows. 抱歉,我没有使用Windows,因此无法测试Windows下的路径是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.