简体   繁体   中英

cmd windows path and environment variables unrecognized in Java Command Prompt

I am trying to execute the following on the command line via the java runtime environment.

Runtime rt = Runtime.getRuntime();

String runtime = "cmd /c start cmd.exe /k \"cd /d C:\\Users\\User\\Documents\\ & python book.py \" "

rt.exec(runtime);

When running the command prompt directly, ie python book.py (assuming that I have already changed the location to the correct directory), python runs fine without any issues.

However, when done through java, the command prompt window looks different, with C:\\WINDOWS\\system32\\cmd.exe instead of displaying Command Prompt.

The above java runtime also gives me 'python' is not recognized as an internal or external command, operable program or batch file. (Whereas the normal command prompt the runs python perfectly fine)

How would I go about including my path and environment variables such that python, or any other path/environment variable, is recognized when I run the command prompt from java?

This may sound naiive but apparently, the solution was to restart the computer. I guess the PATH variables in the command prompt that was run JAVA weren't synchronized like in the other instances of running the command prompt directly.

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