[Python] How can I use arguments containing ";" to subprocess.run?

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I need to launch a JVM from a Python script. For this I'm trying to use subprocess.run. One of the arguments to the JVM is the classpath. In Windows the separator in a Java classpath is the semicolon ;. I need to add several JARs to the classpath.

    This works perfectly fine when I manually launch the JVM from the command line. But when I try to launch it with subprocess.run it seems like the part of the classpath which is after the semicolon is ignored. I suspect this has something to do with the fact that the semicolon is the Windows command separator. Anyone knows how to get around it?

    Failing code

    def run_groovy_script(groovy_script: str, *args):
    # This function calls sys.exit in case of error. The idea is to run several commands in sequence and immediately abort with error message if one fails.
    # Construct the command to run the Groovy script with arguments
    command = ["groovy.bat", "-cp", "tools/slf4j-simple-2.0.13.jar;tools", groovy_script] + list(args)

    try:
    result = subprocess.run(command, check=True)
    except subprocess.CalledProcessError as e:
    print(f"{groovy_script} failed with return code {e.returncode}")
    sys.exit(1)
    except Exception as e:
    print(f"An error occurred executing {groovy_script}: {str(e)}")
    sys.exit(1)


    Successful cmd invocation

    groovy.bat -cp "tools/slf4j-simple-2.0.13.jar;tools" tools/script.groovy build hej

    Continue reading...

Compartilhe esta Página