简体   繁体   中英

executing a python script on windows using java Runtime.exec

I have a python script that runs on windows and uses win32 extensions and WMI to get some information. If I run the script using the command line, it executes perfectly. But, if I try to run the same script using java Runtime.exec("python myscript.py") it seems to get blocked on the waitFor(). The code is like this:

Process p = Runtime.getRuntime().exec("python myscript.py");
int exitCode = p.waitFor();

If I try to use this same java code with some very simple python script like

print "hello world"

I get the exitCode to be 0, which means it works. Can I execute a python script that imports WMI library, using java Runtime.exec()?

Thanks

One likely reason is that your IO buffers are full and need to be flushed, so try flushing both stdout and stderr from your Process in the java code ( example code ).

Alternatively, you could try redirecting all output to NUL or a text file with one of the following arguments to exec:

cmd.exe /c python myscript.py > NUL 2>&1

cmd.exe /c python myscript.py > output.txt 2>&1

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