Execute perl in java

T

Tim

Hi,
I need to execute a perl script from within a java program. The
perl script reads a text file, formats it, then writes it into another
text file. My code for executing the perl script doesn't work at all:

try
{
Process p = Runtime.getRuntime().exec("C:\\perl\\transReader.pl");
}
catch(IOException e)
{
System.out.println(e);
}

When this is run, no errors are thrown but the text file that is
supposed to be written to by the perl script remains empty. If anyone
can help me I'd be extremely grateful.

Thanks, Hill.
 
M

Marco Schmidt

Tim:

[...]
When this is run, no errors are thrown but the text file that is
supposed to be written to by the perl script remains empty. If anyone
can help me I'd be extremely grateful.

The .pl file is not an executable. Either use "start script.pl" (or
"cmd /c start script.pl", Google will come up with more suggestions)
if the file extension .pl is associated with Perl by the operating
system, or use the perl interpreter as first argument (something like
"perl script.pl", I don't know the exact syntax for running
interpreter).

Regards,
Marco
 
T

Tim

The .pl file is not an executable. Either use "start script.pl" (or
"cmd /c start script.pl", Google will come up with more suggestions)
if the file extension .pl is associated with Perl by the operating
system, or use the perl interpreter as first argument (something like
"perl script.pl", I don't know the exact syntax for running
interpreter).

Thanks for the reply Marco,
I've tried using the following two lines of code, but I'm still
having no luck:

Process p = Runtime.getRuntime().exec("C:\\Perl\\perl
transReader.pl");

Process p = Runtime.getRuntime().exec("cmd /c C:\\Perl\\perl
transReader.pl");

I really can't see what I'm doing wrong, if anyone knows, or knows of
a tutorial I could read, please post.
Thanks...
 
A

Anthony Borla

Tim said:
Thanks for the reply Marco,
I've tried using the following two lines of code, but I'm
still having no luck:

Process p = Runtime.getRuntime().exec("C:\\Perl\\perl
transReader.pl");

Process p = Runtime.getRuntime().exec("cmd /c C:\\Perl\\perl
transReader.pl");

I really can't see what I'm doing wrong, if anyone knows,
or knows of a tutorial I could read, please post.
Thanks...

Your launching of the Perl script appears fine. Sometimes a problem that
might be experienced when using 'exec' is with the process blocking because
it generates console output that is then not processed [i.e. read by the
invoking Java program], or, it might be waiting for console input. The way
to remedy this would be to redirect all console output to NUL:

String cmdString
= "cmd.exe /c perl.exe script.pl 2> nul: 1> nul:";

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

and ensure there are no input pending in the script.

This is not the problem in your program since it is not blocking, but
quickly returning. Other things to check:

* Perl executable can be found from Java program launch
location i.e. it is in the PATH [should be ok since you
specify the full path]

* The script can be located. The above code is assuming
that it is located in the Java program launch location -
you may wish to ensure that this is so.

Alternatively, use:

String cmdString
= "cmd.exe /c perl.exe -S script.pl 2> nul: 1> nul:";

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

to locate the script in the PATH

* The final possibility is that the script is broken - it may
be that it can't locate the files it is supposed to use ?
Ensure these all use absolute paths, rather than peforming
any changing of directories

I hope this helps.

Anthony Borla
 
J

John C. Bollinger

Also, don't forget to wait for the Process to finish before you try to
use the results. You do this with Process.waitFor(). Be certain in any
event to waitFor the process at some point, as otherwise you have a
resource leak.


John Bollinger
(e-mail address removed)
 
Joined
Feb 10, 2011
Messages
1
Reaction score
0
the secret is in another place; not in the command

i was stuck onto that same problem for more than a week. I even tried the newer method, ProcessBuilder. But alas, all effort went in vain. :(

Now i discovered that, whatever file the Perl script has created or worked on, has been created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

now :ciao:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top