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.