3

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.

asked Sep 29, 2010 at 17:26

3 Answers 3

1

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.

answered Sep 29, 2010 at 17:43
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

answered Sep 29, 2010 at 17:43

Comments

0

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.

answered Sep 29, 2010 at 17:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.