Maybe the java-program does not write to
Print also the
If you put
Use functions, to avoid code repetition.
Example:
stdout
.Print also the
stderr
from the process.If you put
encoding="utf8"
in the function call subprocess.run
, you don't need to decode the raw bytes, then the function does it for you.Use functions, to avoid code repetition.
Example:
#!/usr/bin/env python3 """ Source for test.jar: http://www.java2s.com/Code/Jar/t/Downloadtestjar.htm """ import subprocess def get_int(question, allowed_values): while True: print("Please choose from", allowed_values) answer = input(question + ": ") try: value = int(answer) except ValueError: print("Input is not an integer") continue if value not in allowed_values: print(value, "is not in allowed_values") continue return value def run_simple_client(info=False, cwd=None): cmd = ["java", "-jar", "test.jar"] if info: cmd.append("-info") return subprocess.run(cmd, capture_output=True, encoding="utf8", cwd=cwd) def main(): choices = (1, 2) choice = get_int("Please enter your choice", choices) print() if choice == 1: print("Without info") proc = run_simple_client() # could be an else-block, but maybe you want to extend the menu ... elif choice == 2: print("With info") proc = run_simple_client(info=True) print("stdout: ", proc.stdout.rstrip()) print("stderr: ", proc.stderr.rstrip()) print("returncode: ", proc.returncode) if __name__ == "__main__": main()
Output:[andre@andre-Fujitsu-i5 ~]$ LANG=C ./jj.py
Please choose from (1, 2)
Please enter your choice: 1
Without info
stdout:
stderr: no main manifest attribute, in test.jar
returncode: 1
Docs:
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!