2

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.

8
  • 4
    What’s the working directory in each case? Commented Nov 28, 2018 at 10:29
  • 2
    Are you absolutely sure about that? Try letting the Perl script print its working directory. And are there *.doc etc files in that directory? Commented Nov 28, 2018 at 10:32
  • 1
    Just to not leave any stone unturned: Do you know what working directory means? I.e. not the location of a script or binary. Commented Nov 28, 2018 at 10:37
  • 1
    use Cwd; my $dir = getcwd; this is command to get working directory Commented Nov 28, 2018 at 10:39
  • 1
    And if you print $dir? Does it print what you expect when run from Java? Commented Nov 28, 2018 at 10:51

1 Answer 1

1

Your Perl script does not set the working directory. I make the assumption that you want to add the contents of the directory E:/perl/scripts to @list. Try to add to the Perl script:

chdir('E:/perl/scripts');

Sorry, I do not use Windows, so I can't test if that is the correct path under Windows.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.