如何使用Java打开命令提示符并插入命令?

问题描述 投票:38回答:9

是否可以打开命令提示符(我猜其他系统的任何其他终端),并在新打开的窗口中执行命令?

目前我拥有的是:

Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{"cmd.exe","/c","start"});

我尝试在“开始”之后添加下一个命令,我尝试运行另一个包含我的命令的rt.exec,但我找不到让它工作的方法。

如果重要,我正在尝试运行类似于此的命令:

java -flag -flag -cp terminal-based-program.jar

编辑不幸的是我有一些奇怪的发现。我已经能够成功启动命令提示符并使用以下命令传递命令:

rt.exec("cmd.exe /c start command");

但是,它似乎只能使用一个命令。因为,如果我尝试使用像这样的命令分隔符“cmd.exe / c start command&command2”,则第二个命令将通过后台传递(如果我刚使用rt.exec(“command2”)的话;) 。现在问题是,我意识到我需要更改运行命令提示符的目录,因为如果我只使用jar文件的完整路径,则jar文件会错误地从命令提示符的活动目录中读取数据,而不是jar的目录,包含其资源。

java command terminal launch
9个回答
23
投票

我知道人们建议远离rt.exec(String),但是这样可行,我不知道如何将其更改为数组版本。

rt.exec("cmd.exe /c cd \""+new_dir+"\" & start cmd.exe /k \"java -flag -flag -cp terminal-based-program.jar\"");

8
投票

如果一次运行两个命令只是为了更改运行命令提示符的目录,则Runtime.exec方法会出现重载,允许您指定当前工作目录。喜欢,

Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start command", null, new File(newDir));

这将在newDir的目录中打开命令提示符。我认为您的解决方案也可以正常工作,但这会使您的命令字符串或数组更加清晰。

将命令作为字符串并将命令作为String数组有一个重载。

但是,您可能会发现使用ProcessBuilder更加容易,directory具有public static void main(String[] args) { try { String ss = null; Process p = Runtime.getRuntime().exec("cmd.exe /c start dir "); BufferedWriter writeer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); writeer.write("dir"); writeer.flush(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); System.out.println("Here is the standard output of the command:\n"); while ((ss = stdInput.readLine()) != null) { System.out.println(ss); } System.out.println("Here is the standard error of the command (if any):\n"); while ((ss = stdError.readLine()) != null) { System.out.println(ss); } } catch (IOException e) { System.out.println("FROM CATCH" + e.toString()); } } 方法来设置当前的工作目录。

希望这可以帮助。


8
投票
Runtime rt = Runtime.getRuntime();
String[] testArgs = {"touch", "TEST"};
rt.exec(testArgs);

3
投票

以下适用于Snow Leopard:

Process pr = rt.exec(arguments);
BufferedReader r = new BufferedReader(new InputStreamReader(pr.getInputStream()));

事实是,如果要读取该命令的输出,则需要读取进程的输入流。例如,

String command = "cmd.exe /c start "+"*your command*";

Process child = Runtime.getRuntime().exec(command);

允许您非常轻松地读取命令的逐行输出。

问题可能还在于MS-DOS不会将您的参数顺序解释为“启动新的命令提示符”。您的数组应该是:

{“start”,“cmd.exe”,“\ c”}

要在新命令提示符中打开命令,您必须使用Process引用。但是我不确定你为什么要这样做才能使用exec,就像我之前评论的人一样。


2
投票

您只需要在启动后在您传递的字符串中附加命令。

Process p = Runtime.getRuntime().exec("cmd.exe /c start dir ");
Process p = Runtime.getRuntime().exec("cmd.exe /c start cd \"E:\\rakhee\\Obligation Extractions\" && dir");
Process p = Runtime.getRuntime().exec("cmd.exe /c start cd \"E:\\oxyzen-workspace\\BrightleafDesktop\\Obligation Extractions\" && dir");

0
投票

您可以在命令提示符上使用任何on进程获取动态路径

Runtime.getRuntime().exec("cmd.exe /c start cmd /k \" parameter \"");

0
投票

请将命令放在如下所述的参数中。

\"

0
投票

你必须仔细设置所有\k(报价)。参数pause用于在执行后保持命令提示符处于打开状态。

1)结合2个命令使用(例如ipconfigRuntime.getRuntime() .exec("cmd /c start cmd.exe /k \"pause && ipconfig\"", null, selectedFile.getParentFile());

MORE

2)显示文件使用的内容(File selectedFile = new File(pathToFile): Runtime.getRuntime() .exec("cmd /c start cmd.exe /k \"MORE \"" + selectedFile.getName() + "\"\"", null, selectedFile.getParentFile()); 是Windows上的命令行查看器)

String[] command = {"cmd.exe" , "/c", "start" , "cmd.exe" , "/k" , "\" dir && ipconfig 
\"" };
ProcessBuilder probuilder = new ProcessBuilder( command );
probuilder.directory(new File("D:\\Folder1"));
Process process = probuilder.start();

一个嵌套引用\“用于命令和文件名,第二个引用\”用于文件名本身,用于名称中的空格等。


0
投票
qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.