I am trying to execute my Perl script from Java program. I have already installed Perl in my computer. Below is my sample Perl file (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.
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.
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.
*.docetc files in that directory?$dir? Does it print what you expect when run from Java?