Thursday, August 14, 2008

Execute a perl script from java Program

Interested to execute a perl script from a java program.... uhmmmmm... see below java program (Sample.java)

import java.io.*;

public class Sample{

public static void main(String args[]) {

Process process;

try
{
process = Runtime.getRuntime().exec("perl testscript.pl");
process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");
}
else
{
System.out.println("Command Failure");
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}

javac Sample.java
java -cp . Sample

17 comments:

Supriya said...

This program works fine,but never shows results generated by execution of perl script.
Plz,can you tell me that how to print
results generated by script onto std.console using a simple java progra,

Vishnu Agrawal said...

Hi Supriya,
Below program will do your job;

import java.io.*;

public class Sample{

public static void main(String args[]) {

Process process;

try
{
process = Runtime.getRuntime().exec("perl 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());
}
}
}

Supriya said...

Hello Vishnu!
Thank You ,your code works really fine!
Thank You Once again.

KARTHIK said...

Hi Vishnu/ Supriya.!

I am not able to execute the perl script from my java program. Can u please help me.

my jave code :

public static void main(String[] args) {
// TODO Auto-generated method stub
String command = "D:\\perlexamples\\hello.pl";
try {
Process process;
Runtime r = Runtime.getRuntime();
process = r.exec("perl D:\\perlexamples\\hello.pl");
process.waitFor();
//Runtime rt = Runtime.getRuntime();
//rt.exec(command);
if(process.exitValue() == 0) {
System.out.println("Process Executed Normally");
} else {
System.out.println("Execution Failed");
}
} catch(Exception excep) {
excep.printStackTrace();
}

}

Vishnu Agrawal said...

Hi Karthik,

This code works fine in solaris box. You are trying to run this program on windows. On a Microsoft Windows platform, all the commands are internal to the command processor so the single command-line argument would be the quoted string: "cmd /c perl args"

try to replace following line in code
process = r.exec("perl D:\\perlexamples\\hello.pl");

with

process = r.exec("cmd /c perl D:\\perlexamples\\hello.pl");

Do remember, perl should be in your system path. I hope, it helps you

Elango said...

Vishnu agarwal,
If the perl program is returning an hash array how to receive that in a structure in java? can you please help me


Elango

anu said...

Hi ,

I used the same program as posted to execute a dummy perl file with code like this :
#!perl -w
use strict;

print "Calling pl file";

exit 0;

it always gives me "command failure". i just wanted to execute a perl file from java code and check whether it works fine or not. Right now, the perl file is on my filesystem in windows n i used "cmd /c perl...." format for giving the filename. Later, i would have to put this all on a Solaris server n run it mainly from there...Also, perl isn't installed on my system but it is on the Solaris server where i hv to finally run it from. Could this failure be due to this reason?

please let me know , i m new to perl too......

thanks.

Unknown said...

Hi

I want to call the perl script from my java class.. but problem is perl script is on different server so i can not use Runtime.getRuntime().exec() .... does anyone have solution for this ??

Nagashree Nootigattu said...

Hi Vishnu -

We have perl script which requires an argument to execute and spits out result (something a conversion utility). I am writing an java app for the users to enter the parameter in the text field and execute the perl script on a mouseclick of the button and display the result on the app not the console.

with your guidance here, I was able to execute a batch file (with hard coded arguments to perl script) and display on the console, can you assist me on taking the arguements dynamically and display the results on the frontend?

Thanks a ton!

Nagashree Nootigattu said...

Actually.. I kept little extra effort and worked it fine...

thanks anyways....

kiran said...

Hi,
Can u gave a procedure... that how to run the same perl script from rempte location ( assuming 10.127.127.127 is the IP of the remote machine and C:\\perl\perl.pl is the script location in the machine)which you have shown in the example

ujwala said...
This comment has been removed by the author.
ujwala said...
This comment has been removed by the author.
PB said...

Hi, How do I pass a file to the perl cmmand insode Exec()..
Please help me!

Unknown said...

Hi Vishnu,

Thanks a lot for sharing this post, this post helps me to compelte my requirement.


can you explain "how we can pass variables to the perl script". I've tried in many ways but cant able to run.

Thanks in advance,
Karthik

Unknown said...

Hi Vishnu,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test2 {
public static void main(String args[]) {



Process process;

try
{
process = Runtime.getRuntime().exec("cmd /c perl C:\\Users\\kiar0116\\Desktop\\main.pl");
process.waitFor();
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(process.exitValue());
System.out.println("Command Failure");
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}
}

I am getting the exit value=1.Command failure.Could you please help how to execute and how to get o/p command execute successfully and have to process another logic.

Thanks.

Unknown said...

main.pl:-

$name = "Ali";
$age = 10;

$status = ($age > 60 )? "A senior citizen" : "Not a senior citizen";

print "$name is - $status\n";