如何通过Java执行cmd命令
我试图通过Java执行命令行参数。例如:如何通过Java执行cmd命令,java,exec,Java,Exec,我试图通过Java执行命令行参数。例如: // Execute command String command = "cmd /c start cmd.exe"; Process child = Runtime.getRuntime().exec(command); // Get output stream to write from it OutputStream out = child.getOutputStream(); out.write("cd C:/ /r/n".getBytes(
// Execute command
String command = "cmd /c start cmd.exe";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("cd C:/ /r/n".getBytes());
out.flush();
out.write("dir /r/n".getBytes());
out.close();
上述命令打开命令行,但不执行cd
或dir
。有什么想法吗?我正在运行Windows XP,JRE6
(我修改了我的问题,使之更加具体。以下答案很有帮助,但没有回答我的问题。)试试这个
您不能使用“cd”来更改运行命令的目录。您需要要运行的可执行文件的完整路径
此外,使用File/directory类更容易列出目录的内容,每次exec调用都会创建一个进程。第二次和第三次调用不会在第一次调用中创建的同一个shell进程中运行。尝试将所有命令放入bat脚本并在一次调用中运行:
rt.exec(“cmd myfile.bat”)
或类似的每次执行exec
都会产生一个具有自己环境的新进程。因此,您的第二次调用没有以任何方式连接到第一次调用。它只需更改自己的工作目录,然后退出(即,它实际上是一个no-op)
如果要编写请求,需要在对exec
的单个调用中完成。Bash允许在一行中指定多个命令(如果它们用分号分隔);Windows CMD可能允许相同的操作,如果不允许,则始终存在批处理脚本
,如果此示例实际上是您想要实现的,那么您可以通过以下方式更高效、更有效、更安全地执行相同的操作:
String[] filenames = new java.io.File("C:/").list();
如果要在cmd shell中运行多个命令,则可以构造如下的单个命令:
rt.exec("cmd /c start cmd.exe /K \"cd c:/ && dir\"");
解释更多。这是因为每个
运行时.exec(..)
返回一个进程类,该类应在执行后使用,而不是由运行时类调用其他命令
如果你看一下,你会发现你可以使用
getInputStream()
getOutputStream()
通过发送连续命令并检索输出,您应该在其上工作。您发布的代码启动三个不同的进程,每个进程都有自己的命令。要打开命令提示符,然后运行命令,请尝试以下操作(本人从未尝试过):
从进程中写入输出流是错误的方向。”在这种情况下,out是指从流程到您。尝试获取/写入进程的输入流,并从输出流中读取以查看结果。我在forums.oracle.com中找到了这一点
允许重用进程在Windows中执行多个命令:
你需要像这样的东西
String[] command =
{
"cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\\ /A /Q");
// write any other commands you want here
stdin.close();
int returnCode = p.waitFor();
System.out.println("Return code = " + returnCode);
同步管道等级:
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try
{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; )
{
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
我也面临同样的问题,因为这里的一些人评论说解决方案对他们不起作用,这里有一个链接,链接到找到有效解决方案的帖子
另请参见使用Cygwin终端的最佳答案中的“更新”,这里是一个不需要多线程的简单示例:
try {
String command = "Command here";
Runtime.getRuntime().exec("cmd /c start cmd.exe /K " + command);
} catch (IOException e) {
e.printStackTrace();
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SimplePty
{
public SimplePty(Process process) throws IOException
{
while (process.isAlive())
{
sync(process.getErrorStream(), System.err);
sync(process.getInputStream(), System.out);
sync(System.in, process.getOutputStream());
}
}
private void sync(InputStream in, OutputStream out) throws IOException
{
while (in.available() > 0)
{
out.write(in.read());
out.flush();
}
}
public static void main( String[] args ) throws IOException
{
String os = System.getProperty("os.name").toLowerCase();
String shell = os.contains("win") ? "cmd" : "bash";
Process process = new ProcessBuilder(shell).start();
new SimplePty(process);
}
}
谢谢这会打开命令行,但它不会执行cd或dir命令。哎呀,我喜欢带有免责声明的代码片段:“我自己从未尝试过。”>\u这会打开命令行,但它不会执行cd或dir命令,为什么?为什么它会被批准……这是错误的!下面的答案是实际答案。乔,如果你仍然觉得你的问题没有得到回答,我认为你应该提供更多的背景资料。我的答案绝对符合您想要实现的目标,即用C:\列出文件。Vincent和Carles的回答向您展示了如何从单个exec()
调用运行多个shell命令。我不确定您现在对什么不满意。答案与问题的代码无关-在我回答时没有“其他”exec调用,但问题已被编辑。请在投票前查看此问题的修订历史。@Boris-检查问题历史,在我发布此答案后的两个月内已对其进行了编辑。很好,链接无效,但复制到此处的代码就足够了。@tvanfosson Hi,我有一个疑问!它适用于“cmd”,我无法将其用于“powershell”。有什么解决方法我可以遵循吗?答案与问题无关。请在你的答案中添加一些解释,以便其他人可以从中学习
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SimplePty
{
public SimplePty(Process process) throws IOException
{
while (process.isAlive())
{
sync(process.getErrorStream(), System.err);
sync(process.getInputStream(), System.out);
sync(System.in, process.getOutputStream());
}
}
private void sync(InputStream in, OutputStream out) throws IOException
{
while (in.available() > 0)
{
out.write(in.read());
out.flush();
}
}
public static void main( String[] args ) throws IOException
{
String os = System.getProperty("os.name").toLowerCase();
String shell = os.contains("win") ? "cmd" : "bash";
Process process = new ProcessBuilder(shell).start();
new SimplePty(process);
}
}