Java Runtime.getRuntime().exec(),隐藏控制台画面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/995298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:59:54  来源:igfitidea点击:

Runtime.getRuntime().exec(), hide the console screen

javaruntime.exec

提问by

I am executing a batch file using Java code. The code is given below:

我正在使用 Java 代码执行批处理文件。代码如下:

Process proc = null;

proc = Runtime.getRuntime().exec("cmd /c start somebat.bat");

With this, the normal command prompt screen gets open. Now I want to suppress/hide the command prompt window(the black one). I found somewhere that if I remove the start attribute from the command, it doesn't appear but when removing it from the command, the command doesn't executes and no exceptions are also shown.

这样,正常的命令提示符屏幕就会打开。现在我想抑制/隐藏命令提示符窗口(黑色的)。我在某处发现,如果我从命令中删除 start 属性,它不会出现,但是当从命令中删除它时,该命令不会执行,也不会显示任何异常。

Can any body tell me how to suppress this window?

任何人都可以告诉我如何抑制这个窗口?

回答by gubby

I dont know windows very well, but I suggest you omit the "cmd" bit of the command. cmd.exe is the windows terminal. Just a guess. Look up the other exec() methods, there is one which takes the command executable to run, and the arguments. On UNIX at least, you can't normally do anything a shell doesn't support (like piping the output to a file) since those are shell features and not done by the called program. Could be why you find if you remove the cmd prefix some things don't work.

我不太了解windows,但我建议你省略命令的“cmd”位。cmd.exe 是 windows 终端。只是一个猜测。查找其他 exec() 方法,有一个需要运行命令可执行文件和参数。至少在 UNIX 上,您通常不能执行 shell 不支持的任何操作(例如将输出通过管道传输到文件),因为这些是 shell 功能而不是由被调用程序完成的。可能就是为什么您会发现如果删除 cmd 前缀,某些事情将不起作用。

try just:

试试吧:

Process proc = Runtime.getRuntime().exec("somebat.bat");

Process proc = Runtime.getRuntime().exec("somebat.bat");

回答by jitter

Add /Q

添加 /Q

Runtime.getRuntime().exec( "cmd /c /Q start somebat.bat");

回答by Brian Agnew

Have you tried

你有没有尝试过

start /min "title" "c:\path\batchfile.bat"

This will run your batch file without the window. It will still appear in the taskbar, however (since it's minimised)

这将在没有窗口的情况下运行您的批处理文件。它仍然会出现在任务栏中,但是(因为它被最小化了)

回答by kgiannakakis

Have a look at this forum post. One of the answers suggest to use a vbs scriptto hide the window.

看看这个论坛帖子。答案之一建议使用vbs 脚本来隐藏窗口。

回答by John Smithers

Have you tried the B option of "start"?

你有没有试过“开始”的B选项?

proc = Runtime.getRuntime().exec("cmd /c start /B somebat.bat");

Edit:
Ok, Anish, that is funny that your code is not executed.
I set up a unit test:

编辑:
好的,Anish,有趣的是你的代码没有被执行。
我设置了一个单元测试:

Process proc = null;
    try
    {
        proc = Runtime.getRuntime().exec("cmd /c start /B D:\temp\_test\somebat.bat");
        proc = Runtime.getRuntime().exec("cmd /c call D:\temp\_test\somebat.bat");
        proc = Runtime.getRuntime().exec("D:\temp\_test\somebat.bat");
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

My somebat.bat file looks like this:

我的 somebat.bat 文件如下所示:

rem somebat.bat:
d:
cd D:\temp\_test
copy somebat.bat somebat2.bat

All three versions in the try-block above work in my scenario. Somebat.bat is copied to somebat2.bat without a command window popping up (what happens, if I use your call, shown in your question).

上面 try-block 中的所有三个版本都适用于我的场景。Somebat.bat 被复制到 somebat2.bat 没有弹出命令窗口(会发生什么,如果我使用你的电话,显示在你的问题中)。

Edit 2:Next round ;-)
Anish, can you show us how your somebat.bat and your ant file looks like?
Because all of the three calls below work in my scenario:

编辑 2:下一轮 ;-)
Anish,你能告诉我们你的 somebat.bat 和你的 ant 文件是什么样的吗?
因为以下所有三个调用都适用于我的场景:

test code:

测试代码:

Process proc = null;
proc = Runtime.getRuntime().exec("cmd /c start /B c:\temp\_test\somebat.bat");
proc = Runtime.getRuntime().exec("cmd /c call c:\temp\_test\somebat.bat");
proc = Runtime.getRuntime().exec("c:\temp\_test\somebat.bat");

somebat.bat:

somebat.bat:

cd\temp\_test  
ant mycopy  

build.xml:

构建.xml:

<?xml version="1.0"?>
<project name="testproj" default="mycopy" basedir=".">
  <target name="mycopy">
      <copy file="myfile.txt" tofile="mycopy.txt" />
  </target>
</project>

myfile.txt: arbitrary text file

myfile.txt:任意文本文件

回答by gustavo

Try this:

尝试这个:

Runtime.getRuntime().exec(cmd.exe /K C:/path/batchfile.bat);

Runtime.getRuntime().exec(cmd.exe /KC:/path/batchfile.bat);

回答by user1768108

Process proc = null;
proc = Runtime.getRuntime().exec("cmd /c start C:\temp\somebat.bat");

回答by erdem

You can use "run" instead of "start".

您可以使用“运行”而不是“开始”。

Runtime.getRuntime().exec("cmd /c run somebat.bat");

Runtime.getRuntime().exec("cmd /c run somebat.bat");