Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a Python app and a Java app running simultaneously. I want Java to start the Python process and then communicate using the normal STDIN/STDOUT streams with Python. I have started the process correctly and have two threads to handle the two I/O streams.

OUTPUT THREAD:

class output2 extends Thread {

Process process;
OutputStream stdin;
BufferedWriter writer;
Scanner in = new Scanner(System.in);

output2(Process p) {
    try {
        process = p;
        stdin = process.getOutputStream();
        writer = new BufferedWriter(new OutputStreamWriter(stdin));
    } catch (Exception e) {
        System.out.println("ERROR output2(): " + e);
    }
}

@Override
public void run() {
    System.out.println("Starting OUTPUT THREAD");
    try {
        while (true) {
            String input = in.nextLine();
            writer.write(input);
            writer.flush();
        }
    } catch (Exception e) {
        System.out.println("ERROR output2_run(): " + e);
    }
    System.out.println("Ending OUTPUT THREAD");
}
}

INPUT THREAD :

class input2 extends Thread {

Process process;
InputStream stdout;
BufferedReader reader;

input2(Process p) {
    try {
        process = p;
        stdout = process.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stdout));
    } catch (Exception e) {
        System.out.println("ERROR input2(): " + e);
    }
}

@Override
public void run() {
    System.out.println("Started INPUT THREAD");
    try {
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is executing");
            if (reader.readLine() != null) {
                System.out.println("Stdout: " + reader.readLine());
            }
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " stopped executing");
        }
    } catch (Exception e) {
        System.out.println("ERROR input2_run(): " + e);
    }
    System.out.println("Ending INPUT THREAD");
}
}

MAIN :

public class My_Java {

public static void main(String args[]) {
    File file = new File("C:\\Location\\");
    try {
        Process process = Runtime.getRuntime().exec("C:\\Python27\\python.exe chat_from_file.py", null, file);
        input2 input = new input2(process);
        output2 output = new output2(process);
        input.setName("INPUT THREAD");
        output.setName("OUTPUT THREAD");
        input.start();
        output.start();
    } catch (Exception e) {
        System.out.println("ERROR main(): " + e);
    }
}
}

This doesn't seem to give any response at all. It starts both threads, says INPUT THREAD is executing but nothing after that. Where am I going wrong?

share|improve this question

1 Answer 1

First of all, after calling if (reader.readLine() != null) { in your input class, you effectively have read the line and the next call will return null.

Use ready to check for non-blocking read possibility. Don't read upfront.

However, I'm pretty sure that you process exists abnormally, with something like python: can't open file 'chat_from_file.py': [Errno 2] No such file or directory or, throws a stacktrace and exits.

Use getErrorStream to check what the process is outputting if an error exists. This will put you on the correct path to solve your issue.

Also, just in case, make sure there's actually something to be read. Make sure your Python application is outputting enough data for buffers to be flushed (or flushing its writes).

And don't forget to join and exit cleanly and correctly. Good luck.

share|improve this answer
    
Is this a valid improvement? while (reader.ready()) { if (reader.readLine() != null) { System.out.println("Stdout: "+reader.readLine()); } else { System.out.println("------"); } } –  Kanishka Ganguly Dec 2 '13 at 18:51
    
No. You can't readLine() twice if there's only one line to read. Use if (reader.ready()) { "Stdout:" + reader.readLine(); }. Did you find out the error Python is throwing by reading its stderr? –  soulseekah Dec 3 '13 at 4:27
    
I added a new thread for the STDERR but that also returns no response, even though it says thread started. Here is the updated code: while (true) { if (reader.ready()) { System.out.println("Stderr:" + reader.readLine()); } Thread.sleep(5000); } –  Kanishka Ganguly Dec 3 '13 at 9:34
    
Okay, try running something else then, something that will surely output something like echo Hello World. –  soulseekah Dec 3 '13 at 9:51
    
It works fine when I run a Python code with just input. It reads just fine. But when I have both input and output expected, it fails. –  Kanishka Ganguly Dec 3 '13 at 9:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.