Call PERL script from JAVA

I am facing a problem in running a PERL script in JAVA in UNIX box..

I am able to call ther perlscript.

Perl script has

##########################

#! /usr/local/bin/perl

print "\nEnter Your Name :";

$name = <>;

print "\nYour Name is : $name\n";

exit 0;

####################################################

Perl script request for the INPUT(name) .

My Java program is

File perlfile = new File("test.pl");

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec("perl "+perlfile);

####################################################

Here is the problem tat IT IS says error =2 ..What has to be the solution so tat i can CALL PERL SCRIPT as similiar to running it separatly in prompt { >perl test.pl }

PLEASE help me on this....

[877 byte] By [Balajeea] at [2007-11-27 7:39:30]
# 1

import java.lang.*;

public class Test2 {

public static void main(String args[]) {

String cmdLine="cmd /c perl test.pl"; //

try {

Process p = Runtime.getRuntime().exec(cmdLine);

} catch(Exception ioe){

System.out.println(ioe);

}

}

}

Try this out and lemme know.....

@@CKM@@a at 2007-7-12 19:20:12 > top of Java-index,Java Essentials,Java Programming...
# 2

And why don't you implement this functionality in Java?

It's not that hard, and it will avoid any cross platform difficulties that will (they have to) occur when using Runtime.exec().

Also, you don't give a file object to the Runtime.exec method. You give it either a String or a String[]. That "File" line is not needed, and counter-productive.

And that aside from any of the problems you may (will?) have with input.

masijade.a at 2007-7-12 19:20:12 > top of Java-index,Java Essentials,Java Programming...
# 3

In the PERL SCRIPT (test.pl)

####################################################

LINE 1 : #! /usr/local/bin/perl

LINE 2 : print "\nEnter Your Name :"; .

LINE 3 : $name = <>;

LINE 4 : print "\nYour Name is : $name\n";

LINE 5 : exit 0;

####################################################

When i run this script in perl test.pl in prompt (UNIX BOX), i am gettin the request for name "Enter Your Name:_ " but when i call this script from Java it doesn't request for name and moreover the process doesnt ends (use ctrl+c to come out of the process).When i come out forcefully it shows the ERROR=2.

####################################################

My requirement is such tat need to call a PERL SCRIPT from java so tat java process give the control to PERL script and it will continue the process..

Sample scenario:

Java move a file and store it in a new FOLDER

MY perl script will read the file in new FOLDER.

here the perl script will get the file name for processing... My issue comes here .$name is not be prompted while calling thro java..

Balajeea at 2007-7-12 19:20:12 > top of Java-index,Java Essentials,Java Programming...
# 4

If you absolutely must run a script (that will be expecting input from the user) then you will probably have to either read from the standard input stream and write what you read to the process, or find some other way to connect the processes Standard In (the getOutputStream from Process) with the Standard InputStream of the Java Process.

You cannot directly interact with the perl script through the console when it is being run with a Runtime.exec. At least AFAIK. I have never tried it, but I am fairly certain that that is the case.

masijade.a at 2007-7-12 19:20:12 > top of Java-index,Java Essentials,Java Programming...
# 5

Dood,

Remember, You're running inside a platform independent Virtual Machine (a coffee flavoured one :-)... unlike C/C++/Perl/*sh or any of the other "native" "programming/scripting" languages you cannot "exec" this process.

If your requirement really is for the java process to cease and the perl process to take on the job from there then you CAN'T DO IT in Java.

But, have a look at url=http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html "When runtime exec won't" for a good rundown on Runtime.execs inherent gotchas.

Cheers. Keith.

Message was edited by: corlettk [url=] doesn't work anymore.

corlettka at 2007-7-12 19:20:12 > top of Java-index,Java Essentials,Java Programming...