如何使用;cd";使用Java运行时的命令?

如何使用;cd";使用Java运行时的命令?,java,terminal,runtime,runtime.exec,cd,Java,Terminal,Runtime,Runtime.exec,Cd,我已经创建了一个独立的java应用程序,在其中我试图使用Ubuntu 10.04终端中的“cd”命令更改目录。我使用了以下代码 String[] command = new String[]{"cd",path}; Process child = Runtime.getRuntime().exec(command, null); 但是上面的代码给出了以下错误 Exception in thread "main" java.io.IOException: Cannot run program "

我已经创建了一个独立的java应用程序,在其中我试图使用Ubuntu 10.04终端中的“cd”命令更改目录。我使用了以下代码

String[] command = new String[]{"cd",path};
Process child = Runtime.getRuntime().exec(command, null);
但是上面的代码给出了以下错误

Exception in thread "main" java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory

有人能告诉我如何实施吗

没有名为
cd
的可执行文件,因为它不能在单独的进程中实现

问题是每个进程都有自己的当前工作目录,而将
cd
作为单独的进程来实现,只会改变处理当前工作目录

在Java程序中,不能更改当前工作目录,也不需要更改。只需使用绝对文件路径

当前工作目录起作用的一种情况是执行外部进程(使用
ProcessBuilder
Runtime.exec()
)。在这些情况下,您可以显式地指定用于新启动的流程的工作目录(分别是和)

注意:当前工作目录可以从
user.dir
读取。您可能会想设置该系统属性。请注意,这样做会导致,因为。

请参阅下面的链接(这说明了如何执行此操作):

i、 e:

String[] cmd = { "/bin/sh", "-c", "cd /var; ls -l" };
Process p = Runtime.getRuntime().exec(cmd);

如果您已经为java运行时研究过这个exec命令,请创建一个具有要“cd”到的路径的文件对象,然后将其作为exec方法的第三个参数输入

public Process exec(String command,
                String[] envp,
                File dir)
         throws IOException
在具有指定环境和工作目录的单独进程中执行指定字符串命令

这是一种方便的方法。表单exec(command,envp,dir)的调用与调用exec(cmdarray,envp,dir)的行为完全相同,其中cmdarray是命令中所有令牌的数组

更准确地说,命令字符串使用由call new StringTokenizer(命令)创建的StringTokenizer分解为标记,而无需进一步修改字符类别。然后,标记器生成的标记按相同顺序放置在新的字符串数组cmdarray中

Parameters:
    command - a specified system command.
    envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
    dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. 
Returns:
    A new Process object for managing the subprocess 
Throws:
    SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess 
    IOException - If an I/O error occurs 
    NullPointerException - If command is null, or one of the elements of envp is null 
    IllegalArgumentException - If command is empty

这个命令运行得很好

Runtime.getRuntime().exec(sh -c 'cd /path/to/dir && ProgToExecute)
试用:

Runtime.getRuntime.exec("cmd /c cd path"); 
这起作用了

Runtime r = Runtime.getRuntime(); 
r.exec("cmd /c pdftk C:\\tmp\\trashhtml_to_pdf\\b.pdf C:\\tmp\\trashhtml_to_pdf\\a.pdf cat output C:\\tmp\\trashhtml_to_pdf\\d.pdf"); 
以下选项不起作用 在使用数组时,该命令不起作用

String[] cmd = {"cmd /c pdftk C:\\tmp\\trashhtml_to_pdf\\b.pdf C:\\tmp\\trashhtml_to_pdf\\a.pdf cat output C:\\tmp\\trashhtml_to_pdf\\d.pdf"}; r.exec(cmd);

仅供参考,我正在使用实用程序检查其上面的窗口是否适用于除windows removecmd/c

之外的其他操作系统。我已通过让Java应用程序执行同一目录中的sh脚本解决了这个问题,然后在sh脚本中完成了“cd”


我需要对特定目录执行“cd”,以便目标应用程序能够正常工作。

使用process builder的方法之一,我们可以传递我们希望执行cmd的目录。请参见下面的示例。此外,您还可以使用wait-for方法说明进程的超时

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmd).directory(new File(path));

        Process p = builder.start();

        p.waitFor(timeoutSec, TimeUnit.SECONDS);

在上面的代码中,您可以将路径[我们希望执行cmd的位置]的file对象传递给我使用runtime.exec()的ProcessBuilder的目录方法。你能告诉我如何明确指定工作目录吗?@Antro@Jigar我以前已经看到过这个问题。但是解决方案使用Executer,但我只想使用Runtime.use Runtime.getRuntime.exec(“cmd/c cd path”);感谢@DheerajSachan
Runtime r=Runtime.getRuntime()
r.exec(“cmd/c pdftk c:\\tmp\\trashhtml_to_pdf\\b.pdf c:\\tmp\\trashhtml_to_pdf\\a.pdf cat输出c:\\tmp\\trashhtml_to_pdf\\d.pdf”)使用数组命令时无效
String[]cmd={“cmd/c pdftk c:\\tmp\\trashhtml\u to_pdf\\b.pdf c:\\tmp\\trashhtml\u to_pdf\\a.pdf cat输出c:\\tmp\\trashhtml\u to_pdf\\d.pdf”}
r.exec(cmd)一种逃避方法是启动一个新的shell并启动所有命令,它们的命令-我不遵循,不应该是
Runtime.getRuntime().exec(“sh-c'cd/path/to/dir&&ProgToExecute')
?这将返回exit code
2
这应该是我认为的答案是的,这是对我有效的答案:但是,请记住,File dir参数需要使用包含的最后一个斜杠来指定。例如:File dir=新文件(“C:\\l\\workspace\\myproject\\mydirectory\”;