gpt4 book ai didi

java - 是否可以从java更改CMD目录?如果是这样,如何? (如 CMD 中的 "CD")

转载 作者:行者123 更新时间:2023-12-04 05:35:48 28 4
gpt4 key购买 nike

我制作了 2 个 .java 文件和 1 个 .bat 文件。让我们将第一个 .java 文件称为“First.java”,将第二个称为“Second.java”。批处理文件是“compile.bat”。假设 First.java 位于“.../Desktop/temp/First.java”,而 Second.java 位于“.../Desktop/Test/Second.java”以及一个批处理文件(“.. ./Desktop/Test/compile.bat”)。现在,compile.bat 包含以下代码:

cmd /c "cd C:\blahblah\temp && javac First.java"

我在 CMD 和双击中对其进行了测试,它有效。我想用以下代码从 Second.java 编译 First.java:
Process p1 = Runtime.getRuntime().exec("C:\\blahblah\\compile.bat");

(我必须导航到 dir,因为 CMD 的默认目录与 compile.bat 的目录不同)。这不起作用。它似乎只是忽略了代码。正如我所说,我尝试了很多不同的东西,我什至尝试使用其他一些应该更改目录的库。请帮帮我!

最佳答案

最简单的方法:你应该使用 start .设置工作目录也是可以的,看这个例子:

Runtime r=Runtime.getRuntime();
r.exec("cmd.exe /c start compile.bat", //path to executable
null, // env vars, null means pass parent env
new File("C:\\blahblah")); // working directory

附加信息:
如果您不想在单独的控制台中启动您的进程(这就是 start 所做的),您可以执行 r.exec("cmd.exe /c compile.bat"); ,但因为它在其父控制台的上下文中执行,您 必须等待 p.waitFor()或读取它的输入流 - 否则它可能会默默地失败。
这将运行并显示您命令的输出:
Process p=r.exec("cmd.exe /c compile.bat",         //path to executable
null, // env vars, null means pass parent env
new File("C:\\blahblah"));

InputStream is=p.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is));

String line=new String();
while ((line=br.readLine())!=null) System.out.println (line);

同样从 1.5 ProcessBuilder 开始是首选的进程启动方式 (来自 Java 文档):

java.lang.Process

The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts.

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.

The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.

There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.

As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.

Since: JDK1.0

关于java - 是否可以从java更改CMD目录?如果是这样,如何? (如 CMD 中的 "CD"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951697/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com