简体   繁体   English

Java命令提示符中无法识别的cmd Windows路径和环境变量

[英]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. 我试图通过Java运行时环境在命令行上执行以下命令。

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. 当直接运行命令提示符时,即python book.py(假设我已经将位置更改为正确的目录),python运行正常,没有任何问题。

However, when done through java, the command prompt window looks different, with C:\\WINDOWS\\system32\\cmd.exe instead of displaying Command Prompt. 但是,通过Java完成操作后,命令提示符窗口看起来与C:\\ WINDOWS \\ system32 \\ cmd.exe不同,而不是显示命令提示符。

The above java runtime also gives me 'python' is not recognized as an internal or external command, operable program or batch file. 上面的java运行时也给我“ python”,它不被识别为内部或外部命令,可操作程序或批处理文件。 (Whereas the normal command prompt the runs python perfectly fine) (而普通命令提示运行python完全正常)

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? 从Java运行命令提示符时,如何识别我的路径和环境变量,例如python或任何其他路径/环境变量?

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. 我猜想运行JAVA的命令提示符中的PATH变量没有像直接运行命令提示符的其他实例那样同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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