![](/img/trans.png)
[英]Scrapy command in shell script not executing when called from java
[英]Perl script is not fully executing when called from Java Program
我正在尝试从Java程序执行我的Perl脚本。 我已经在计算机上安装了Perl。 以下是我的示例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";
这是我执行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());
}
现在首先,当我尝试运行此Java类时,我的输出低于输出。
Command Successful
Value of list = a list of lines
但是当我使用命令从命令提示符运行此脚本时
perl test.pl
我的输出如下:
Value of list = a list of lines one.doc test.pl photo.jpg
因此,我从Java程序执行的Perl脚本输出与从命令提示符得到的输出不同。
您的Perl脚本未设置工作目录。 我假设您想将目录E:/perl/scripts
的内容添加到@list
。 尝试添加到Perl脚本中:
chdir('E:/perl/scripts');
抱歉,我没有使用Windows,因此无法测试Windows下的路径是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.