【问题标题】:The system cannot find the file specified java系统找不到指定java的文件
【发布时间】:2015-05-20 16:55:22
【问题描述】:

是的,我已经知道这个问题是重复的,但请耐心等待。其他问题都没有回答这个问题。

这是我的代码:

package pc.setup;

import java.io.IOException;

public class DirectoryCreator {
    public static void setupDirectories() throws IOException {
        Runtime.getRuntime().exec("cd\\");
    }
}

这是我得到的错误:

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

【问题讨论】:

  • 你在什么操作系统上运行你的代码?
  • 太棒了!!!我的代码示例对你有用!

标签: java cmd java-io


【解决方案1】:

这里有两个问题:

  • cd 本身不是可执行文件;它是命令外壳的内置功能。 exec 只运行可执行文件(在它们自己的文件中)。这就是为什么它没有被发现。 exec 可以运行命令 shell,但是 ...
  • 即使您确实在命令 shell 中更改了目录,该更改也仅对新生成的进程有效,对启动它的程序无效。

抱歉,这种方法在 Java 中不起作用。

【讨论】:

    【解决方案2】:

    我假设您在 Windows 操作系统系列下运行此应用程序(否则您将不得不稍微调整一下)。 Runtime.exec() 方法用于执行可执行命令。 dir, cd, copy, 并非如此......因此,它会触发您正在经历的异常。

    cd 命令是 Windows 命令解释器cmd. 的一部分,它可以使用command.comcmd.exe 执行,具体取决于操作系统的版本。

    因此,我们应该首先运行命令解释器,并将用户提供的命令(例如dir,cd,copy, ...)提供给它

    这是一个执行此操作的程序。它检查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();
              }
        }
    }
    

    这是这个类的简单跑步者

    public class GoodWindowsExecRunner {
    
        public static void main(String[] args) {
            //GoodWindowsExec.main(new String[] {"dir *.*"});
            GoodWindowsExec.main(new String[] {"cd .."});
        }
    
    }
    

    这是执行“cd ..”命令的结果

    这是执行“dir .”命令的结果

    有一篇关于java世界的优秀文章叫When Runtime.exec won't

    【讨论】:

    • 一切都很好,但是启动一个命令 shell 只是为了执行 cd 仍然不会完成任何有用的事情。
    • @EJP,这不是卢卡斯问的吗?如果我回答了他的问题,我认为你没有正当理由拒绝我的回答
    • 如果它不是有用的东西,我怀疑有人会在 javaworld 上写一篇关于它的多页文章以及在各种网站上写许多其他文章
    【解决方案3】:

    谢谢大家的回答。不过,它们很罗嗦,所以在这个答案中,我将尝试对其进行总结。

    当您调用Runtime.getRuntime.exec() 时,您必须指定您使用的shell(仅在Windows 上)。所以,你会说Runtime.getRuntime.exec("command here", "cmd.exe")。您可能知道,CMD 是现代 Windows 操作系统的 Windows 命令外壳。

    再次感谢您的回答。

    【讨论】:

    • 但是exec方法没有(String, String)构造函数。
    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 2023-03-17
    • 2017-12-21
    • 2015-09-28
    • 2015-11-08
    • 2018-08-26
    • 2019-01-20
    相关资源
    最近更新 更多