使用Java,在同一cmd.exe窗口中运行多个命令 [英] With Java, run multiple commands in the same cmd.exe window

查看:1278
本文介绍了使用Java,在同一cmd.exe窗口中运行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java应用程序,它将在Windows计算机上偶尔运行。
在某些时候,我需要运行一个Cygwin提示符,并在其中执行一些命令。

I'm developing a Java application that will be run on a Windows computer occasionally. At some point I need to run a Cygwin prompt and performs some commands in it.

我发现了一个使用Runtime类的主题:
http://www.javaquery。 com / 2011/03 / how-to-execute-multiple-command-in.html

I've found a topic where the Runtime class is used: http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html

但是它不会启动真实的 cmd .exe 窗口,它只在后台运行,输出只是打印在Eclipse控制台上。

However it doesn't launch a real cmd.exe window, it's only run in background and the output is just printed on the Eclipse console.

我正在寻找一个解决方案来运行一个真正的 cmd.exe 窗口,我需要传递尽可能多的命令,我想要的Windows shell。
这是可能吗?

I'm looking for a solution to run a real cmd.exe window and I need to pass as many commands as I want to that windows shell. Is this possible?

推荐答案

这个工程...使用&&

This one works... using && operator you can add one or commands to be executed in same command prompt

try {
    Process p = Runtime
                    .getRuntime()
                    .exec("cmd /c start cmd.exe /K \"dir && ping localhost && echo end\"");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

考虑这里

从提问者更新:解决方案在cygwin中执行命令

getRuntime().exec("cmd /c start C:/cygwin64/bin/bash.exe --login -c \"ls ; whoami ; exec bash\"");

这篇关于使用Java,在同一cmd.exe窗口中运行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆