By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Getting Python exit code when calling Python script from Java program

P: n/a
I have a Python script which is used to load data into a database. Up to
now this script has been run by customers from the Windows command
prompt using "python edg_loader.pyc". Any error messages generated are
written to a log file. A project team working in the same company as me
here would like to use this loading utility. They write UI applications
for Windows using Java. They were able to launch the Python script from
within Java by creating a Process using Java ProcessBuilder class.
However, the way the error handling is currently implemented isn't
really suitable for use in a UI application. As I'm sure you can
appreciate it's not really feasible to tell users of a UI program to
keep checking the log files while the loading is underway!!. Ideally
they would like the Python loading utility to return an error code and
error message - the error message could then be displayed on a message
box on the UI.
I seem to be having problems implementing this. I tried using the
sys.exit() method in my script and passed non -zero values. However the
value wasn't picked up the by Java Process.exitValue() method - it kept
picking up 0. On investigation it turned out that the exit value being
read is from python.exe process, not from the Python script. Is there
any way I can obtain the return value of a python script from a Java
program?

I did manage to get some sort of return error message. I wrote a test
message to sys.stderr in the Python script and this was picked up by
Java Process.getErrorSteam() method.
However I would really like to get the return codes working if possible
and would appreciate any suggestions on how to implement this.

Thanks,
Patricia Quill

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Lie
On Jun 18, 3:54*pm, Quill_Patri...@emc.com wrote:
I have a Python script which is used to load data into a database. Up to
now this script has been run by customers from the Windows command
prompt using "python edg_loader.pyc". Any error messages generated are
written to a log file. *A project team working in the same company as me
here would like to use this loading utility. They write UI applications
for Windows using Java. They were able to launch the Python script from
within Java by creating a Process using Java ProcessBuilder class.
However, the way the error handling is currently implemented isn't
really suitable for use in a UI application. As I'm sure you can
appreciate it's not really feasible to tell users of a UI program to
keep checking the log files while the loading is underway!!. Ideally
they would like the Python loading utility to return an error code and
error message - the error message could then be displayed on a message
box on the UI.
I seem to be having problems implementing this. I tried using the
sys.exit() method in my script and passed non -zero values. However the
value wasn't picked up the by Java Process.exitValue() method - it kept
picking up 0. On investigation it turned out that the exit value being
read is from python.exe process, not from the Python script. Is there
any way I can obtain the return value of a python script from a Java
program?

I did manage to get some sort of return error message. I wrote a test
message to sys.stderr in the Python script and this was picked up by
Java Process.getErrorSteam() method.
However I would really like to get the return codes working if possible
and would appreciate any suggestions on how to implement this.

Thanks,
Patricia Quill
I'm not experienced in Java and Python, but if all else fails, you
could always create a file (or append to the log file) a special
string that indicates what the problem or whether it runs
successfully. The GUI application would always check this file after
script execution
Jun 27 '08 #2

P: n/a
On 2008-06-18, Qu************@emc.com <Qu************@emc.comwrote:
I have a Python script which is used to load data into a database. Up to
now this script has been run by customers from the Windows command
prompt using "python edg_loader.pyc". Any error messages generated are
written to a log file. A project team working in the same company as me
here would like to use this loading utility. They write UI applications
for Windows using Java. They were able to launch the Python script from
within Java by creating a Process using Java ProcessBuilder class.
However, the way the error handling is currently implemented isn't
really suitable for use in a UI application. As I'm sure you can
appreciate it's not really feasible to tell users of a UI program to
keep checking the log files while the loading is underway!!. Ideally
they would like the Python loading utility to return an error code and
error message - the error message could then be displayed on a message
box on the UI.
The first thing to do is decide whether this is a Java problem, a Python
problem, or a OS problem. Then post the question in the appropiate forum.

One simple experiment may be to write a C function that returns a non-zero exit
code, and run that as job.
I seem to be having problems implementing this. I tried using the
sys.exit() method in my script and passed non -zero values. However the
value wasn't picked up the by Java Process.exitValue() method - it kept
What did the OS say?
Run a program like below in OS ('python x.py'), then query the OS about the
exit code of the program. In that way, you can narrow down your search.
picking up 0. On investigation it turned out that the exit value being
read is from python.exe process, not from the Python script. Is there
any way I can obtain the return value of a python script from a Java
This is not what I see happening here:

x.py:
import sys
sys.exit(138)

% python2.4 x.py
% echo $?
138

as you can see, the mechanism works at my Linux system.

Java Process.getErrorSteam() method.
However I would really like to get the return codes working if possible
and would appreciate any suggestions on how to implement this.
I'd suggest to first find out where in the Java->OS->Python->OS->Java chain
things go wrong.

As for how to handle a child-process from Java, try asking in a Java news
group.

Sincerely,
Albert
Jun 27 '08 #3

P: n/a
In article <ma*************************************@python.or g>,
I tried using the sys.exit() method in my script and passed non -zero
values. However the value wasn't picked up the by Java
Process.exitValue() method - it kept picking up 0. On investigation
it turned out that the exit value being read is from python.exe
process, not from the Python script.
I don't believe there is any such distinction. The exit status of
python.exe is the exit status determined by the script.

-M-

Jun 27 '08 #4

P: n/a
En Wed, 18 Jun 2008 08:09:58 -0300, A.T.Hofkamp <ha*@se-162.se.wtb.tue.nl>
escribió:
On 2008-06-18, Qu************@emc.com <Qu************@emc.comwrote:
>picking up 0. On investigation it turned out that the exit value being
read is from python.exe process, not from the Python script. Is there
any way I can obtain the return value of a python script from a Java

This is not what I see happening here:

x.py:
import sys
sys.exit(138)

% python2.4 x.py
% echo $?
138

as you can see, the mechanism works at my Linux system.
It works fine on Windows too, the OS she appears to be using:

C:\TEMP>python x.py

C:\TEMP>echo %ERRORLEVEL%
138

--
Gabriel Genellina

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.