views:

2755

answers:

6

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

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.

Can any body tell me how to suppress this window?

A: 

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.

try just:

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

jgubby
Hi,The given option not working. The bat file is not executed.
Did you try specifying the full path?
jgubby
Yes I tried but the batch file didnt executed.
A: 

Add /Q

Runtime.getRuntime().exec( "cmd /c /Q start somebat.bat");
jitter
Hi jitter,/Q option is not working. The command is not executed when I add /Q :-(
A: 

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)

Brian Agnew
Hi Brian,I need to completely hide the prompt screen. This option will just minimize the window.
A: 

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

kgiannakakis
+1  A: 

Have you tried the B option of "start"?

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:

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:

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).

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:

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: arbitrary text file

John Smithers
Hi JohnNopes /B also doesnt resolve my problem. The batch file is not executed.
Actually an ant command is being executed in my batch file. It is only getting executed with following call. With other options it never get executed."cmd /c start D:\\temp\\_test\\somebat.bat"
If you are executing only an ant command you may also consider just doing that directly from java: all Ant tasks can be accessed as classes and just run (given proper configuration) and Ant itself can also be invoked from within Java (see for example: http://www.ibm.com/developerworks/websphere/library/techarticles/0502_gawor/0502_gawor.html )
Boris Terzic
A: 

Hi John,

The ant command looks like:

ant -lib ./ant-lib -f checkstyle_build.xml -verbose -Dcode.location=%1 -Dcode.dir=%2 %3

where %1, %2, %3 are the arguements given when running the batch file. I also tried putting the arguements directly in the call above but then also its not running.

Thanks for so much effort you putting on giving me the resolution. I really appreciate it.

Regards

Anish