Home > Tech Tips > Hide batch command window

Hide batch command window

If you are trying to run a Java Swing application by clicking on a batch file in Windows, you will see an empty command window and the swing application. There is no one magic command that will hide the command window, but here is a little script that will do the trick –

Files: MyApp.jar, AppStart.bat, run.bat

AppStart.bat
java -jar MyApp.jar

Run.bat
@echo off
setlocal
set VBS=”%TEMP%\HideCmdWindow.vbs”
if exist %VBS% goto Runit
@echo dim obj, obj1, obj2, objArgument>%VBS%
@echo Set WshShell = WScript.CreateObject(“WScript.Shell”)>>%VBS%
@echo Set objArgument = Wscript.Arguments>>%VBS%
@echo obj = WshShell.Run(“AppStart.bat“, 0)>>%VBS%
@echo set WshShell = Nothing>>%VBS%
:Runit
set param=%*
set param=”%param:”=“`%”
cscript //nologo %VBS% %param%
endlocal

Start your application by clicking on run.bat and the command window should be hidden. You should implement this only if you do not wish to see the debug/error statements thrown to the command window.

Source: http://www.jsifaq.com/SF/Tips/Tip.aspx?id=9242

Share

Categories: Tech Tips Tags:
  1. sandesh
    March 12th, 2008 at 01:40 | #1

    I am trying to use this command to hide my batch file
    i given
    AppStart.bat- javaw -classpath %classpath%;../CESoftMarch11.jar commonUI.MainUI

    Run.bat is same
    all three(Appstart,run,jar)are in same folder but application is not running
    :(

  2. May 7th, 2008 at 04:08 | #2

    Hello, you use the copy and paste …. replace all -> ” by new.

    goodbye …

  3. Fabian
    September 24th, 2008 at 03:57 | #3

    Hi, thanks for your code.
    But it doesn’t work for me.
    (WinXP Pro VMware)

    This code works. Maybe others have the same problem.

    Greatings

    @echo off
    setlocal
    set VBS=%TEMP%\HideCmdWindow.vbs
    if exist %VBS% goto Runit
    @echo dim obj, obj1, obj2, objArgument>%VBS%
    @echo Set WshShell = WScript.CreateObject(“WScript.Shell”)>>%VBS%
    @echo Set objArgument = Wscript.Arguments>>%VBS%
    @echo obj = WshShell.Run(“AppStart.bat”, 0)>>%VBS%
    @echo set WshShell = Nothing>>%VBS%
    :Runit
    set param=%*
    set param=”%param:”=“`%”
    cscript //nologo %VBS% %param%
    endlocal

  4. smp
    March 12th, 2009 at 15:47 | #4

    Thanks. It worked for me!!!

  5. Abhimanyu
    April 21st, 2009 at 06:45 | #5

    how is the argument passing working?

    I see you have passed the parameters to script
    :cscript //nologo %VBS% %param%

    however in the script:
    @echo Set objArgument = Wscript.Arguments>>%VBS%
    @echo obj = WshShell.Run(”AppStart.bat”, 0)>>%VBS%

    parameters are not being passed to the bat file.

    has anyone done it?

  6. kishore
    August 28th, 2009 at 11:06 | #6

    Thanks alot. Worked great.

  7. kishore
    August 28th, 2009 at 11:12 | #7

    I found this worked on my system and failed to run on another windows system. It seems like cscript failed torun HideCmdWindow.vbs since %VBS% has a name in 8.3 format that truncated the name. As a result it failed to run cscript.
    So i debugged it furthur and i found another simple soultion to hide the window. use start command with /SEPARATE option
    heres the example. I’m invoking another bacth file from one batch file that brings up my swing application.
    start /SEPARATE /MIN AABCDEFGH.bat.
    don’t forget to exit your parent batch file and child batch files.

    Hope this helps.

  1. No trackbacks yet.