2

I have a Java program, reading a file (which has characters from a native language), and populating a string. It works fine, when program is run directly.

But when same program is invoked from Python then its not able to populate the string.

        public static void main(String[] args) {
        File inputFile = new File("input.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile),"UTF-8"));
      string output = "";
        while ((line = br.readLine()) != null) {
           // This block never hits when invoked by python. It works fine when java program runs directly.
           output +=line+" ";
         }
         ...
         }

From Python I am invoking it as following

cmd = ['java', java_class]
subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

Any inputs? btw I am using Atom IDE, not sure if that makes any difference.

Gupta
  • 41
  • 4
  • What is the error that you are getting? – The Roy Mar 09 '19 at 13:54
  • no error. Its just that my output comes empty instead of having contents of file – Gupta Mar 09 '19 at 14:13
  • Check this post https://stackoverflow.com/questions/2388423/calling-java-app-with-subprocess-from-python-and-reading-the-java-app-output – The Roy Mar 09 '19 at 14:14
  • @TheRoy, thanks for sharing the link. But not sure if this link helps, it just tells that how to read the java program's output in python. I am able to invoke the java application from python. Issue is that when java application is invoked from python then its not able to read the content of a file, which is perfectly readable when java application is run directly. – Gupta Mar 09 '19 at 14:18

1 Answers1

1

I tried your example and it worked for me. Let's see if it works for you. I will then respond to you what I think about the issue.

import java.io.*;


public class Python2JavaMessaging {
        public static void main(String[] args) {
                try {
                        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
                        PrintWriter writer = new PrintWriter("result.txt", "UTF-8");
                        String s = bufferRead.readLine();
                        while(s.equals("x")==false) {
                                writer.println(s);
                                s = bufferRead.readLine();
                        }
                        writer.close();
                } catch(IOException e) {
                        e.printStackTrace();
                }
        }
}

The Python script is as below:

#!/usr/bin/python

import subprocess

cmd = ['java', '-classpath', '.' , 'Python2JavaMessaging']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf8')

p.stdin.write("First line\r\n")
p.stdin.write("Second line\r\n")
p.stdin.write("x\r\n") # this line will not be printed into the file
The Roy
  • 2,178
  • 1
  • 17
  • 33
  • btw, I have used stdin to write. We can change it to a file as well. In your case, input.txt – The Roy Mar 09 '19 at 15:14
  • Can you please help me by providing the sample with a file. Also, just to highlight again if my file content is english then everything works fine. It does not work only when i have file content in some native language. – Gupta Mar 09 '19 at 15:16
  • you will need to use the right encoding for that. I have used encoding in my example. Try your native language specific encoding. – The Roy Mar 09 '19 at 15:17
  • Check this post for writing to a file. https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously – The Roy Mar 09 '19 at 15:18
  • If you think your query is resolved with these, then please accept the answer to close the thread. – The Roy Mar 09 '19 at 15:19
  • I have tried the exact code that you have shared, but result.txt is empty for me – Gupta Mar 09 '19 at 15:29
  • Can you make sure that the java file as well as the python files are running in the same directory? – The Roy Mar 09 '19 at 15:42
  • yes they are placed in the same directory, thats why result.txt has been created, right? – Gupta Mar 09 '19 at 16:04
  • The only other thing you may want to check is - 1. You run the java program first 2. You make sure your python version is 3+ – The Roy Mar 09 '19 at 16:20
  • What does you mean run the java program first. It is the python script which is going to run the java program. Right? – Gupta Mar 09 '19 at 17:32