小编Pot*_*hil的帖子

Runtime.exec没有运行cd

调用Runtime.getRuntime().EXEC()

当我传递命令"cd filename"
完整代码时,前者不起作用;

package com.piomnicron.riles;

import java.io.File;

import javax.swing.JOptionPane;

public class A {
protected static Runtime B = Runtime.getRuntime();

public static void main(String[] args) {
    File E = new File("");
    System.err.println(E.getAbsolutePath());
    try{
        B.exec("cd "+E.getAbsolutePath()+"\\");

    }catch(Throwable e)
    {
        JOptionPane.showMessageDialog(null, "had an oopsie!");
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

我的问题恰好是,为什么它抛出以下IOException:

cannot run program "cd": CreateProcess error = 2, The system could not find the file specified
Run Code Online (Sandbox Code Playgroud)

我先尝试没有+"\",以防有人认为这可能是答案,

已经做了一些谷歌搜索,并没有一个答案,我发现回答我的问题以任何方式,他们都专注于打开一个罐子,或文件,但我只是想知道为什么cd命令不起作用.我不能使用CD的绝对路径,因为这意味着如果有人移动它所包含的文件夹,它将会中断.

错误是B.exec(); 如果你想知道的话

请原谅任何语法错误,太阳在我眼中,我几乎看不到屏幕.

java ioexception

4
推荐指数
1
解决办法
4976
查看次数

标签 统计

ioexception ×1

java ×1