python under java2

M

motokoit

For some reason i need to start a python script from inside a java code. The
main part of the code is something like this

try{
Runtime rt = Runtime.getRuntime();
System.out.println("start");
Process proc = Runtime.getRuntime().exec("python myscript.py");
proc.waitFor();
System.out.println("end");
} catch (Exception e){
e.printStackTrace();
}

and so worked for months....but since some days ago the same instruction
does not work but execute without any exception, that is print either
"start" and "end"; the strangest thing happen when I call just

Process proc = Runtime.getRuntime().exec("python myscript.py");

the execution looks like stalled and does not comes to an end printing just
the "start".

Some suggestion?
My machine is a Linux Mandrake64 10.1
 
S

Steve Horsley

motokoit said:
For some reason i need to start a python script from inside a java code. The
main part of the code is something like this

try{
Runtime rt = Runtime.getRuntime();
System.out.println("start");
Process proc = Runtime.getRuntime().exec("python myscript.py");
proc.waitFor();
System.out.println("end");
} catch (Exception e){
e.printStackTrace();
}

and so worked for months....but since some days ago the same instruction
does not work but execute without any exception, that is print either
"start" and "end"; the strangest thing happen when I call just

Process proc = Runtime.getRuntime().exec("python myscript.py");

the execution looks like stalled and does not comes to an end printing just
the "start".

Some suggestion?
My machine is a Linux Mandrake64 10.1
I think this is probably really a java question. I suspect that your
python script is issuing too much output either on stdout or stderr,
and because you are not reading these streams, the process is blocking
because the output buffers are full. If this is the case, you need to
get your java program to read the streams (Process.getInputStream()
and Process.getErrorStream()), which is something you should be doing
anyway. In order to read both streams, you either need two threads, or
to use java.nio and select.

A short term fix might be to run theh python script by hand and see if
it throws out a long stack trace (this could block an unread error stream).

HTH
Steve
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top