How python script calls Java jar file it?
HelloWorld.java
public class HelloWorld { public static void main(String[] args) { System.out.println("hello world"); String input = args[0]; String output = input.toUpperCase(); System.out.print(output); } }
javacaller.py
# python3 # coding = utf-8 from subprocess import Popen, PIPE, STDOUT mycmd = ['java', '-jar', 'HelloWorld.jar', 'logan'] pipe = Popen(mycmd, stdout=PIPE, stderr=STDOUT) for line in pipe.stdout: print(line, type(line)) if isinstance(line, bytes): line_decode = line.decode('utf-8') print('decode:', line_decode, type(line_decode)) print('done')
Output:
b'hello world\n' <class 'bytes'>
decode: hello world
<class 'str'>
b'LOGAN' <class 'bytes'>
decode: LOGAN <class 'str'>
done
important point:
1. java of the System. Out.println own wrap
The results returned by the need to decode 2. java
References:
How to get the output from .jar execution in python codes?
Reproduced in: https: //www.cnblogs.com/gattaca/p/7364934.html