1

Possible Duplicate:
How should I call a Perl Script in Java?

I have a perl file which is used for remotely checking ip addresses.I need to run that perl in java.please help me to proceed.

Community
  • 1
  • 1
sreejith
  • 11
  • 1
  • 2
  • go through this you will get it http://stackoverflow.com/questions/603554/how-should-i-call-a-perl-script-in-java – run Nov 04 '11 at 10:29
  • use `Runtime.exec` You'll need the Perl parser's path and the script's path. Documentation [here](http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html) – abhinav Nov 04 '11 at 10:31

2 Answers2

1

You need the Runtime.getRuntime().exec() method. Some points to consider:

  • you must explicitly call perl in the command line: "perl myprog.pl". The implicit interpreter selection that the kernel normally does doesn't work in the exec() method.
  • you must make sure that the path to perl is in your program's PATH environment, or in the environment that you pass to the exec() call
  • you must completely drain the stdout and the stderr of the Process that you created, or the call will not terminate.

(The ProcessBuilder class mentioned above simplifies some of these issues.)

Axeman
  • 29,660
  • 2
  • 47
  • 102
Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
1

ProcessBuilder example

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.commons.io.FileUtils;
    OutputStreamWriter writer =null;
    BufferedReader stdoutBR = null;
    try {
        File tmp = new File("temp_dir");
                    File cmdFile = File.createTempFile("foo", ".sh", tmp);
        File stdout = File.createTempFile("foo_stdout", ".txt", tmp);
        File stderr = File.createTempFile("foo_stderr", ".txt", tmp);

        String script = "/usr/bin/perl foo.pl 2>"+stderr.getAbsolutePath()+" >"+stdout.getAbsolutePath()+" \n";
        cmdFile.setExecutable(true);
        FileUtils.writeStringToFile(cmdFile, script);           

        ProcessBuilder processBuilder = new ProcessBuilder(cmdFile.getAbsolutePath());
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        InputStream numbStream = process.getInputStream();
        stdoutBR = new BufferedReader(new InputStreamReader(numbStream));

        String line = null;
        StringBuilder unexpectedOutput = new StringBuilder();
        while ((line = stdoutBR.readLine()) !=null) {
            unexpectedOutput.append(line);
            unexpectedOutput.append("\n");

        }
        process.waitFor();
        stdoutBR.close();
        log.debug("Process exit value:"+process.exitValue());
        if (process.exitValue() != 0) {
            String stdoutString = FileUtils.readFileToString(stdout);
            String stderrString = FileUtils.readFileToString(stderr);
            throw new RuntimeException("Problem executing script. \nOutput:"+unexpectedOutput.toString()+"\nStdout:"+stdoutString+"\nStderr:"+stderrString);
        }

        String output = FileUtils.readFileToString(stdout);
                    FileUtils.deleteQuietly(cmdFile); 
        FileUtils.deleteQuietly(stdout);
        FileUtils.deleteQuietly(stderr);

    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        }
        catch (Exception e) {
            //TODO
        }
    }
bpgergo
  • 15,669
  • 5
  • 44
  • 68