-1

I am trying to run this perl script through java. below is my script

public class Log {
    public static void main(String[] args) throws IOException {
        Process proc =null;
        try
        {
            String[] commandAndArgs = {
                "cmd","/c","C:\\Users\\myscipt.pl"
            };
            proc = Runtime.getRuntime().exec(commandAndArgs);
            int returncode = proc.waitFor();
            if(proc.exitValue() == 0)
            {
                System.out.println("Command Successful");
                try
                {
                    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                    String line="";

                    System.out.println("Process Executed"+returncode);
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else{
                System.out.println("Process Executed"+returncode);
                System.out.println("Command Failure");
            }
        }
        catch(Exception t)
        {
            t.printStackTrace();
            System.out.println("Exception: "+ t.toString());
        }
        finally
        {
            proc.destroy();
        }
    }
}

So when i execute this script its runs perfectly. But as soon as i am replacing the script with below line

"perl","C:\\Users\\myscipt.pl"

It throws me return code 2 error. So, where am i wrong ?

Miller
  • 34,962
  • 4
  • 39
  • 60
Kushal Kumar
  • 101
  • 2

3 Answers3

0

Use this:

Process proc = Runtime.getRuntime().exec("perl C:\\Users\\myscipt.pl");
Miguel Prz
  • 13,718
  • 29
  • 42
0

You can't figure out what really has gone wrong when you get an error return code from a child process unless you read the child process input and error streams simaltaneously, and monitor them.

Importantly, you wait for a process only after you ask your parent process to consume the child process input/error streams, since the documentation of waitFor() says:

causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

And after the child process returns a code, you will not able to read its streams as you have done in the if(proc.exitValue() == 0) loop.

So in order to achieve the above, you could slightly modify the code as below, though i have not read the error stream of the child process, which you should ideally do. To include that you can refer to:

Catching Perl exception in Java

The modified code,

Process proc =null;
try
{
    proc = Runtime.getRuntime().exec("perl C:\\Users\\myscipt.pl");
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String line="";
    while ((line = input.readLine()) != null) {
        System.out.println(line);
        }   
    int returncode = proc.waitFor();
    System.out.println("Process Executed"+returncode);
}
catch(Exception t)
{
    t.printStackTrace();
    System.out.println("Exception: "+ t.toString());
}
finally
{   if(proc != null)
    proc.destroy();     
}

Once you read the error stream of the child process, you would know what error has occured which made the process return that code.

Community
  • 1
  • 1
BatScream
  • 19,260
  • 4
  • 52
  • 68
0

So, i got the solution. Instead of just putting

"perl","C:\Users\myscipt.pl"

i replaced "perl" with full perl path i.e

"C:\Users\Perl64\bin\perl.exe","C:\Users\myscipt.pl"

and it worked

Kushal Kumar
  • 101
  • 2