java 系统找不到java指定的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29113042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The system cannot find the file specified java
提问by Lucas Baizer
Yes, I already know this question is a duplicate, but just bear with me here. None of the other questions answered this.
是的,我已经知道这个问题是重复的,但请耐心等待。其他问题都没有回答这个问题。
This is my code:
这是我的代码:
package pc.setup;
import java.io.IOException;
public class DirectoryCreator {
public static void setupDirectories() throws IOException {
Runtime.getRuntime().exec("cd\");
}
}
This is the error I get:
这是我得到的错误:
Exception in thread "main" java.io.IOException: Cannot run program "cd\": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at pc.setup.DirectoryCreator.setupDirectories(DirectoryCreator.java:7)
at pc.load.PieClickerRunner.main(PieClickerRunner.java:9)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 6 more
采纳答案by Lucas Baizer
Thank you everyone for your answers. They were wordy, though, so in this answer I will try to summarize it.
谢谢大家的回答。不过,它们很罗嗦,所以在这个答案中,我将尝试对其进行总结。
When you call Runtime.getRuntime.exec()
, you must specify what shell you are using (only on Windows). So, you would say Runtime.getRuntime.exec("command here", "cmd.exe")
. As you probably know, CMD is the Windows command shell for modern Windows operating systems.
当您调用 时Runtime.getRuntime.exec()
,您必须指定您使用的外壳程序(仅在 Windows 上)。所以,你会说Runtime.getRuntime.exec("command here", "cmd.exe")
。您可能知道,CMD 是现代 Windows 操作系统的 Windows 命令外壳。
Once again, thank you for your answers.
再次感谢您的回答。
回答by Arthur Dent
There are two problems here:
这里有两个问题:
- cd is not an executable on its own; it is a built-in of the command shell.
exec
only runs executables (in their own files). That is why it is not found.exec
can run a command shell, but ... - Even if you did change directory in a command shell, that change is only in effect for the newly spawned process, not for the program that launched it.
- cd 本身不是可执行文件;它是命令外壳的内置程序。
exec
只运行可执行文件(在它们自己的文件中)。这就是它找不到的原因。exec
可以运行命令外壳,但是... - 即使您确实在命令 shell 中更改了目录,该更改也仅对新生成的进程有效,而不对启动它的程序有效。
Sorry, but that approach does not work in Java.
抱歉,这种方法在 Java 中不起作用。
回答by alainlompo
I am assuming that you are running this application under Windows operating systems family (otherwise you will have to adapt it a little bit). The Runtime.exec()
method is used to execute executable commands. It is not the case with dir,
cd,
copy,
... As a consequence of this it triggers an exception which is what you are experiencing.
我假设您正在 Windows 操作系统系列下运行此应用程序(否则您将不得不对其进行一些调整)。该Runtime.exec()
方法用于执行可执行命令。情况并非如此dir,
cd,
copy,
......因此,它会触发您正在经历的异常。
The cd
command is part of the windows command interpreter cmd.
It can either be executed with command.com
or cmd.exe
depending on the version of the operating system.
该cd
命令是窗口命令的一部分解释cmd.
它可以与被执行command.com
或cmd.exe
取决于操作系统的版本。
Therefore we should run the command interpreter first and feed it the user - supplied command (such as dir,
cd,
copy,
...)
因此,我们应该首先运行命令解释器并将用户提供的命令(例如dir,
cd,
copy,
...)
Here is a program that does that. It checks the os.name
environment variable in order to select the right command interpreter. I test for Windows NT and Windows 95. If none of these two is found but is still a Windows operating system, then I assume it is a modern windows (such as Windows 7 or Windows 8) and it is using cmd.exe.
这是一个可以做到这一点的程序。它检查os.name
环境变量以选择正确的命令解释器。我测试了 Windows NT 和 Windows 95。如果这两个都没有找到但仍然是 Windows 操作系统,那么我假设它是现代 Windows(例如 Windows 7 或 Windows 8)并且它正在使用cmd.exe.
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class GoodWindowsExec
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try
{
String osName = System.getProperty("os.name" );
System.out.println("OS NAME IS " + osName);
String[] cmd = new String[3];
if( osName.equals( "Windows NT" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
else if( osName.equals( "Windows 95" ) )
{
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
} else if (osName.toUpperCase().trim().contains("WINDOWS")) {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
Here is a Simple runner for this class
这是这个课程的一个简单的跑步者
public class GoodWindowsExecRunner {
public static void main(String[] args) {
//GoodWindowsExec.main(new String[] {"dir *.*"});
GoodWindowsExec.main(new String[] {"cd .."});
}
}
Here is the result from executing the "cd .." command
这是执行“cd ..”命令的结果
Here is the result from executing a "dir ." command
这是执行“dir .”命令的结果
There is an excellent article on java world called When Runtime.exec won't
有一篇关于 java 世界的优秀文章叫做When Runtime.exec won't