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.

4
  • What is the error that you are getting? Commented Mar 9, 2019 at 13:54
  • no error. Its just that my output comes empty instead of having contents of file Commented Mar 9, 2019 at 14:13
  • Check this post stackoverflow.com/questions/2388423/… Commented Mar 9, 2019 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. Commented Mar 9, 2019 at 14:18

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

10 Comments

btw, I have used stdin to write. We can change it to a file as well. In your case, input.txt
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.
you will need to use the right encoding for that. I have used encoding in my example. Try your native language specific encoding.
Check this post for writing to a file. stackoverflow.com/questions/15535240/…
If you think your query is resolved with these, then please accept the answer to close the thread.
|

Your Answer

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.