views:

42

answers:

3

Hello:

I developed a command-line utility which needs to be called from a Java GUI application. The team in charge on the Java GUI would like to bind my command-line application to a button in the GUI; the Python application is such that at the time we have no time or interest in rewriting it in Java.

I have no experience whatsoever in Java, so I ask you:

What is the best way to bind a command-line based Python application to a button in a Java-based GUI application?

I am very concerned about exception management (how to tell Java that Python failed).

Thanks.

+1  A: 

You should be able to execute a spawned process from Java using Runtime.exec(). Here's some examples.

Make sure you capture the stdout and stderr (concurrently - see this answer for more details) so you can report on errors. You can capture the exit code of the application, so make sure that the application itself correctly reports errors. The error code would be a more reliable way of detecting errors (I would suspect) thatn parsing the output streams.

Brian Agnew
Thank you Brian. I agree - it is best to ensure that the Python script returns exit codes (integers) rather than messages. Many thanks.
Arrieta
A: 

Have you considered jython? You can: 1) use it to run python scripts (allowing it to call Java classes) 2) compile python into class files, making them usable by normal Java code without jython being present at runtime I've only used it in the first pattern, but I've seen tonnes of docs on the second.

phlip
A: 

I agree with phlip - you can create a scripting engine in Java and use that to call your Python code. Providing that your python code doesn't use any unusual OS-specific library calls or DLLs, this should work fine. This link: http://jythonpodcast.hostjava.net/jythonbook/chapter10.html should give you more information on the exact mechanism you need.

alpian