473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Getting Python exit code when calling Python script from Java program

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
4 6156
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mikey_boy | last post by:
Hello! Curious if anyone could give me a hand. I wrote this PHP script with makes a simple connection to a mysql database called firstdb and just pulls back the results and displays on the...
3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
1
by: chandan | last post by:
Is it possible to call an external java program from a trigger in oracle 8i. I know that you can call a stored procedure in java from a trigger but i want to know is it possible to call an...
4
by: Mark | last post by:
Hi All, I'm trying my hardest to learn VBA but have run into a problem which hopefully someone can guide me with. Basically what I am trying to achieve is to get the database to check the users...
5
by: Noozer | last post by:
I have two Classes in use in an ASP application... The first Class is named "ORDER". It represents one job order. It has a Delete method, among others, which deletes it from my database. The...
3
by: dburdick | last post by:
I was referred to this forum as this may be a php specific problem. Some kind of configuration problem maybe. I have some php files that connect to a mysql database and are run as cron jobs. ...
5
bartonc
by: bartonc | last post by:
Two new sub forums have been added to the Python Forum. These are NOT Q&A forums, but threads will be open to discussion. In Python > Python Code you will find snippets submitted by experts and...
5
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when the user puts a "sys.exit(0)" into his script to...
0
by: rakeshc | last post by:
Hi, I am a new guy in jasper and iReport. I could create a report and subreport successfully with iReport. while running through iReport, it will ask for parameters and after supplying them,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.