Runtime.getRuntime (). Exec (), hides the console screen

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

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

This opens the screen of the normal command line. Now I want to suppress / hide the command prompt window (black). I found somewhere that if I remove the start attribute from the command, it does not appear, but when the command is removed from the command, the command does not execute and exceptions are not displayed.

Can someone tell me how to suppress this window?

+4
source share
8 answers

Have you tried option B "start"?

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

Edit:
Ok, Anish, it's funny that your code is not executing.
I installed unit test:

 Process proc = null; try { proc = Runtime.getRuntime().exec("cmd /c start /BD:\\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:

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

All three versions in the try block above work in my script. Somebat.bat is copied to the somebat2.bat file without the command window appearing (what happens if I use your call specified in your question).

Edit 2: Next round ;-)
Anish, can you show us what your somebat.bat file and your ant file look like?
Since all three calls below work in my scenario:

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:

 cd\temp\_test ant mycopy 

build.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: custom text file

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

Add /Q

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

You tried

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

This will launch your batch file without a window. However, it will still appear on the taskbar (as it is minimized)

0
source

Take a look at this post. One answer suggests using a vbs script to hide a window.

0
source

Try the following:

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

0
source

You can use run instead of start.

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

0
source

I don’t know the windows very well, but I suggest you omit the "cmd" bit of the command. cmd.exe is a Windows terminal. Just a hunch. See other exec () methods, there is one that runs the executable file of the command and arguments. On UNIX, at least you usually cannot do anything that the shell does not support (for example, pipelining to a file), because these are shell functions and are not executed by the called program. Maybe why you find that you remove the cmd prefix, some things do not work.

try simply:

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

-2
source

Source: https://habr.com/ru/post/1286222/


All Articles