简体   繁体   中英

Cannot run 'python -c ".." ' command from Java Process builder and Linux

I was developing my spring boot server on Windows. Now I have upgraded to Ubuntu 20.04.

the project executes a python script which should return a result as a txt file with this command:

 python3 -c "from main import *;main(function,'/tmp/execution12480676806364930620/executionResponse.txt')"

Thanks to this code:

    List<String> items = Arrays.asList(project.getExecutorType().buildAndGetExecutionCommandByProject(project));
    ProcessBuilder pb = new ProcessBuilder(items);
    pb.directory(new File(project.getPath()));
    Process p = pb.start();
    p.waitFor(5, TimeUnit.SECONDS);

when I print in the console the array passed in the Item variable:

[python3, -c, "from main import *;main(function, '/tmp/execution12480676806364930620/executionResponse.txt')"]

and the path of the array passed in pb.directory:

/tmp/execution12480676806364930620

My problem is that the project is not running and returning nothing.

when i go to the folder and run the same command from terminal everything works.

And that on windows 10 this same process worked fine.

Looking at similar issues I modified my code like this but it doesn't change anything:

    List<String> items = Arrays.asList(project.getExecutorType().buildAndGetExecutionCommandByProject(project));
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(items);
    pb.redirectErrorStream(true);
    pb.directory(new File(project.getPath()));
    Process p = pb.start();
    p.waitFor(5, TimeUnit.SECONDS);

What am I doing wrong?


Edit:

My command for read outputs:

private String inputStreamToString(InputStream inputStream){
    return new BufferedReader(
            new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
            .collect(Collectors.joining("\n"));
}

And I call it like that:

System.out.println(this.inputStreamToString(p.getErrorStream()));
System.out.println(this.inputStreamToString(p.getInputStream()));

What works, when I just run "python main.py" I get the errors and print them out.

I can easily add the command at the end of the main file but I don't understand why the python -c "..." is not working? I am not receiving any errors... I manage several languages and this could be a problem for me later

Aha. On closer inspection I think you're right it's not executing anything (and thus not producing any output either normal or error for you to see).

You don't show how the array of strings is created, but your printout suggests you have actually put quotemarks in the third string. That's wrong. When you give the shell command line python -c "import this; dothat" the shell uses the quotemarks to control parsing of this command line, but it does not pass them to the python process; the args passed to the python process (shown vertically for clarity, and omitting the argv[0]=program used in C but omitted in Java) are actually

-c
import this; dothat

If you pass an argument actually containing quotemarks like

-c
"import this; dothat"

then python doesn't execute the commands import and dothat ; instead it evaluates the string literal "import this; dothat" and (since it isn't running interactively) discards the result.

Try not including, or removing, the " at the beginning and end. But leave the ' inside the string value because you do want python to receive those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM