Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Runtime.getRuntime.exec()错误代码_Java - Fatal编程技术网

Java Runtime.getRuntime.exec()错误代码

Java Runtime.getRuntime.exec()错误代码,java,Java,我试图在Java应用程序中使用runtime.getruntime.exec 很长一段时间以来,我一直在尝试运行不同的命令,并且不断得到错误代码2,我发现这意味着文件或目录不存在。为了进行测试,我试图传递一个基本命令,并得到错误代码1。为什么我会得到这个,错误代码1是什么意思 这是我的密码: private String executeCommand(String command) { logger.info("executing command : " + command);

我试图在Java应用程序中使用runtime.getruntime.exec

很长一段时间以来,我一直在尝试运行不同的命令,并且不断得到错误代码2,我发现这意味着文件或目录不存在。为了进行测试,我试图传递一个基本命令,并得到错误代码1。为什么我会得到这个,错误代码1是什么意思

这是我的密码:

  private String executeCommand(String command) {
    logger.info("executing command : " + command);
    String result = null;
    try {
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec(command);

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
        String line = null;

        while ((line = input.readLine()) != null) {
            result = result + line;
        }

        while ((line = stdError.readLine()) != null) {
            result = result + line;
        }
        int exitVal = pr.waitFor();
        System.out.println("Exited with error code " + exitVal);


    } catch (IOException e) {
        e.printStackTrace();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (Throwable e) {
        e.printStackTrace();

    }
    logger.info("This is the result:" + result);
    return result;
我这样称呼它:

 String temp = executeCommand("cd $HOME/my-directory/my-subdirectory");
以下是我的输出:

 INFO : programname - executing command : cd $HOME/my-directory/my-
        subdirectory
 Exited with error code 1
 INFO : programname - This is the result:null/usr/bin/cd[8]: $HOME/my-
        directory/my-subdirectory:  not found

Do
processpr=rt.exec(“cmd/c”+命令)

Runtime.getRuntime().exec(命令)
需要一个exe文件作为传递字符串中的第一个参数。类似(
cd、echo等…
)的命令是特定于命令行工具的,不会像直接传递的命令那样工作。您需要首先调用命令行工具,然后将命令作为参数传递:

// Invoke Command Line Tool (.exe is optional) (cmd for windows sh for Linux)
// 1st argument indicates you want to run a command (/C for windows -c for Linux)
// 2nd argument is the cmd line command to run (echo)
Runtime.getRuntime().exec("cmd.exe /C echo helloworld");
Runtime.getRuntime().exec("sh -c echo helloworld");

另作说明。您将希望将结果初始化为空字符串,而不是null。否则,将在打印输出的内容前面加上“null”一词。

尝试将home变量括起来,然后执行
String temp=executeCommand(“cd${home}/my directory/my subdirectory”)无更改。另外,如果我自己在命令行上运行该命令,并尝试运行
String temp=executeCommand(“/usr/bin/cd$HOME/my directory/my subdirectory”)无更改我在unix服务器上运行此操作。它只是说不能运行程序“cmd”,我正在unix服务器上运行它。它只是说,无法运行程序“cmd.exe”我使用了sh,我仍然得到相同的错误代码。所有的改变是,它现在说的不是“找不到”,而是“不存在”。但是,此目录是我运行程序的目录,因此它确实存在。此外,我所有的代码2错误现在都是代码1,并表示“无法打开”,而不是“没有这样的文件或目录”。我需要用户名和密码才能进入此unix服务器。sh是否撤销了某些权限或类似的内容?另一篇stackoverflow文章描述了在出现“无法打开”错误时执行此操作的更好方法。