Click to See Complete Forum and Search --> : executing perl from java
Kereng
February 6th, 2001, 02:14 AM
Hi all,
I'm trying to execute a perl script (that sends mail) from a java program. This script has to run when an exception is invoked by the program
(the program should first turn the script to an exe, this is what the perl2exe does(it works if I’m running it without the java program), and afterwards runs the exe)
This is the code I've written to do so but it doesn't work
catch(IOException e) {
try{
Process p=Runtime.getRuntime().exec("cmd /c perl2exe mail.pl");
p=Runtime.getRuntime().exec("c:>cmd /c mail");
try{
p.waitFor();
}catch(InterruptedException e4){}
BufferedReader reader=new BufferedReader(new InputStreamReader (p.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
please help...
thx
Geert Arys
February 6th, 2001, 03:34 AM
Hi,
What happens is the following: you create a process perl2exe (BTW why do you need cmd /c ?). While running, you try to create a new process c:>cmd /c mail, (leave the c:>, and again, is the cmd /c needed, it is a normal exe?) and assign the new process to the same variable (you don't have a reference to the original one now.)
Problems:
(1) Chances are 99% that perl2exe did not finish yet before the cmd /c mail was started. So, I've put an extra p.waitFor() in it.
(2) Because you do nothing with the IOException, you're in the dark what happened.
(3) Don't put your reading code of the Perl output in the InterruptedExce
Kereng
February 6th, 2001, 05:15 AM
Hi, and thanks for your quick reply.
I've tried to put another p.waitFor(), and it still doesn't work.
can you please help me with the code, and maybe try to show it to me, what should I do exactly? It'll be very helpfull
Thanks so much again.
Keren
Geert Arys
February 6th, 2001, 05:46 AM
Hi Keren,
My post was almost fully cut off. I gave code, but it is now lost, you may have noticed my last sentence is cut. Try this:
try{
Process p=Runtime.getRuntime().exec("cmd /c perl2exe mail.pl");
p.waitFor();
p=Runtime.getRuntime().exec("cmd /c mail"
Geert Arys
February 6th, 2001, 05:48 AM
Hi Keren,
ANOTHER TRY (if this one does not work, I'll do this from another computer later this day):
try{
Process p=Runtime.getRuntime().exec("cmd /c perl2exe mail.pl");
p.waitFor();
p=Runtime.getRuntime().exec("cmd /c mail")</font%3
Geert Arys
February 6th, 2001, 05:50 AM
Okay, Experiencing problems. My posts get cut off. Expect full code later. Sorry
Regards,
Geert Arys
Kereng
February 6th, 2001, 09:29 AM
Hi again,
I've tried what you've written and itstill doesn't work (for some strange reason).
Do you maybe have any other idea?
or another code I can use?
Thanks again
Keren
Geert Arys
February 6th, 2001, 12:06 PM
Hi Keren,
I've had problems posting. My code was in fact longer. Please try this and, if any exception occurs or if it does not work, please post me what your computer says.
Here we go (again). Hope my post does not get cut this time... :-)
try{
Process p=Runtime.getRuntime().exec("cmd /c perl2exe mail.pl");
p.waitFor();
p=Runtime.getRuntime().exec("cmd /c mail");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader (p.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}
System.out.println("done.");
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch(IOException e1) {
e1.printStackTrace();
}
Regards,
Geert Arys
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.