This is code I've taken from another website, and adapted to work with the JFileChooser. I got it working so that someone could select a java file with the chooser, and I'd send a "java -jar FILENAME" to the console and the app would launch.

However as soon as I tried to use python, I ran into problems. NB: Python works fine if I type it directly into the console, and I can confirm that everything I need is installed (OS I'm using is Ubuntu 10.04).

$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

In my program, if I replace:
System.out.println(sendToConsole("python"));
with:
System.out.println(sendToConsole("echo Hello, World!"));

It works fine, and I get

Hello, World!
DONE...

But using the word 'python' (to launch python) in my Java program doesn't output anything. Here's the code I'm using:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.Runtime;

import javax.swing.JFileChooser;

public class TestConsoleOutput
{
	public static void main(String[] args)
	{
		TestConsoleOutput tc = new TestClass();
		tc.exe();
	}
	
	private void exe()
	{
		System.out.println(sendToConsole("python"));
		System.out.println("DONE...");
	}
	
	private String sendToConsole(String command)
	{
		StringBuffer message = new StringBuffer();
		
		try
		{
			Process process = Runtime.getRuntime().exec(command);
			InputStream inputStream = process.getInputStream();
			InputStream errorStream = process.getErrorStream();
			BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(inputStream));
			BufferedReader bufferedError = new BufferedReader(new InputStreamReader(errorStream));
			//StringBuffer message = new StringBuffer();
			while (true)
			{
				if (inputStream.available() > 0)
				{
					String lineIn;
					while ((lineIn = bufferedInput.readLine()) != null)
					{
						message.append(lineIn);
						message.append(System.getProperty("line.separator"));
					}
				}
				if (errorStream.available() > 0)
				{
					String lineIn;
					while ((lineIn = bufferedError.readLine()) != null)
					{
						message.append("ERROR: ");
						message.append(lineIn);
						message.append(System.getProperty("line.separator"));
					}
				}
				try
				{
					process.exitValue();
					break;
				}
				catch (Throwable throwable)
				{
					Thread.sleep(1000);
				}
			}
			bufferedInput.close();
			bufferedError.close();
		}
		catch (Throwable throwable)
		{
			throwable.printStackTrace();
		}
		
		return (message.toString());
	}
}

Any ideas how I can:
* Grab the output from python
* Add additional input to python

?

I'll be posting a thread in the Python section with a link back here as well.

my Java program doesn't output anything

no error messages?

No error messages, nope. Not even the "DONE..." message, as if it's waiting for a reply from the "python" command or something.

All compiles fine and as I said, if I change the "python" command to be "echo Hello" instead everything works fine.

Edit: To clarify... it seems to be continuously looping. Need to stop it manually.

What do you seen in a terminal window if you enter python?

The command python alone starts an interactive interpreter in a console. You should try sendToConsole("python FILENAME.py") which executes a python program and don't enter interactive mode.

I was hoping to interact with that interpreter by sending more commands to the console depending on what the output was.

I suppose the only way to do that would be to write a python program (.py)? Problem with that is I can't do anything with the output inside my Java app... which was the whole point! :)

*Sigh*

I was hoping to interact with that interpreter by sending more commands to the console depending on what the output was.

I suppose the only way to do that would be to write a python program (.py)? Problem with that is I can't do anything with the output inside my Java app... which was the whole point! :)

*Sigh*

If the python program prints output to the console, can't you read this output from your java program ?

Another possibility is to start an XMLRPC server in your python program and connect to this server from java. This allows you to communicate with a python process from any language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.