无法 运行 编程“...”:CreateProcess 错误=2

Cannot run program "...": CreateProcess error=2

我正在尝试使用 java 执行 cmd 命令,但为什么这个命令不起作用?当我在 windows cmd 中尝试它时,它可以工作,但是当我想用 Java 执行它时,它就不起作用了。

String cmd = "cd "+System.getenv("APPDATA")+"\.minecraft"
Process pc = Runtime.getRuntime().exec(cmds);

你知道为什么吗?

//编辑: 错误信息:

java.io.IOException: Cannot run program "cd C:\Users\Adrian\AppData\Roaming.minecraft": CreateProcess error=2, File not found... at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at at.freakey.thundriallauncheerr.Launcher$SwingAction.actionPerformed(Launcher.java:313) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access0(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.io.IOException: CreateProcess error=2, Das System kann die angegebene Datei nicht finden at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 41 more

我认为 exec 正在检查文件 "cd C:\Users\Adrian\AppData\Roaming.minecraft" 是否存在。

而不是执行 "cd C:\Users\Adrian\AppData\Roaming.minecraft" 使用正确版本的 exec 方法:

exec(java.lang.String command, java.lang.String[] envp,java.io.File workDir)

例如:

exec("executablefilename", null, "C:\Users\Adrian\AppData\Roaming.minecraft")

cd 不是您可以执行的程序。即使可以,它也不会做 nothing.

当您 exec 时,将启动一个新进程。这个新进程独立于您的进程(Java 进程),并且拥有自己的 "current directory"。更改该进程中的当前目录将不会影响Java进程的当前目录。

cd 是命令行程序 cmd.exe 内置 命令。要运行一个cd命令,你需要运行cmd.exe /c cd ...。但是正如我上面所说的那样,这将毫无意义(该过程将立即结束)。

至于改变Java进程的当前目录,看这个:Changing the current working directory in Java?