Python Forum
printing out the contents aftre subprocess.call()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing out the contents aftre subprocess.call()
#1
I am facing a scenario where in I need to execute a jar based on user parameters. I have replicated the scenario here as below :

import subprocess

a = input('Enter either 2 or 3:')

value = int(a)
if a == 2:
    p1 = subprocess.run(['java', '-jar', 'SimpleClient.jar', '-info'], capture_output=True)
    print(p1.stdout.decode())

else:
    p1 = subprocess.run(['java', '-jar', 'SimpleClient.jar'], capture_output=True)
    print(p1.stdout.decode())


print('-----------stdout---------------------')



print('-----------code---------------------')
print(p1.returncode)
What I observe is that the print function does not execute. I expect it to show different results based on my entry.
Error:
Enter either 2 or 3: 2 -----------stdout--------------------- -----------code--------------------- 0
I had a doubt if p1 is only within the if and else scope. I put the print stattements even inside those clauses. But that does not help either.

Could anyone guide me here?
Reply
#2
The order of the things you're printing is odd (you print the separators back to back, while the output is printed before them), but otherwise seems reasonable.

My first thought would be that the program is not producing anything on stdout. Maybe it is all going to stderr or some other location. You can test this by replacing it with something that you know has stdout. When I did that, it printed just fine (albeit before the stdout separator).
Reply
#3
(Jul-29-2021, 04:05 PM)bowlofred Wrote: The order of the things you're printing is odd (you print the separators back to back, while the output is printed before them), but otherwise seems reasonable.

My first thought would be that the program is not producing anything on stdout. Maybe it is all going to stderr or some other location. You can test this by replacing it with something that you know has stdout. When I did that, it printed just fine (albeit before the stdout separator).

yes you are right. It is captured udnder std.err
I am confused here as in, the log messages from the jar which are of type INFO are also a part of the err attribute..
Any reason for that?
Reply
#4
Maybe the java-program does not write to 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:
Gribouillis and Rakshan like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  continue if 'subprocess.call' failes tester_V 11 5,146 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing contents of Jar on timeout Rakshan 1 1,718 Jul-30-2021, 07:48 AM
Last Post: buran
  subprocess call cannot find the file specified RRR 6 16,543 Oct-15-2020, 11:29 AM
Last Post: RRR
  Why wont subprocess call work? steve_shambles 3 2,661 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  printing a list contents without brackets in a print statement paracelx 1 2,115 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  subprocess.call - Help ! sniper6 0 1,516 Nov-27-2019, 07:42 PM
Last Post: sniper6
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,914 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  Echo call to VLC bash using subprocess.Popen on Linux LuukS001 17 9,674 Jan-07-2019, 03:58 AM
Last Post: LuukS001
  Why is subprocess.call command not working? zBernie 5 10,728 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Help with subprocess..call oldcity 1 2,540 Sep-28-2018, 03:59 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020