Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting Python exit code when calling Python script from Java program

Reply
Thread Tools

Getting Python exit code when calling Python script from Java program

 
 
Quill_Patricia@emc.com
Guest
Posts: n/a
 
      06-18-2008
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

 
Reply With Quote
 
 
 
 
Lie
Guest
Posts: n/a
 
      06-18-2008
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
 
Reply With Quote
 
 
 
 
Matthew Woodcraft
Guest
Posts: n/a
 
      06-18-2008
In article <mailman.590.1213782173.1044.python->,
> 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-

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-18-2008
En Wed, 18 Jun 2008 08:09:58 -0300, A.T.Hofkamp <>
escribió:
> On 2008-06-18, <> wrote:


>> 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(13
>
> % 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I get the exit code "n" passed to sys.exit(n) ? Yujo Python 2 04-10-2007 08:35 PM
In a Perl script 'exit 1' returns exit status 0! kaleem Perl Misc 8 12-14-2006 12:52 AM
Exit code of a batch (using exit /B) Joe Smith Java 4 11-08-2006 12:25 PM
Code to Exit Web App and Exit Internet Explorer =?Utf-8?B?U2FuZHk=?= ASP .Net 7 08-05-2005 01:55 AM
Urgent: Calling a method of a java object (getting a boolean parameter) from java script Eyal Javascript 2 08-07-2003 11:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57