Closing Java console in a .bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Waynes
Posts: 2
Joined: 19 Aug 2013 05:14

Closing Java console in a .bat file

#1 Post by Waynes » 23 Aug 2013 08:16

Hi,

I work for a local council that for some reason has purchased an application that is very flaky when using Java. Certain web screens will not launch and the only way I have found to resolve the problem is to manually open up the Java Console and immediately close it prior to launching the web page. Unfortunately, I have been told that this is not a satisfactory fix when hundreds of people are going to be using the system.

Anyway….I have written a batch file that will open & close the console prior to opening the IE URL(see below).


Start /wait "" "C:\Program Files\Java\jre6\bin\javacpl.exe"
ECHO Opening JAVA Console

timeout 3
TASKKILL /F /IM javacpl.exe


The problem with this is that this kills any/every session of Java that is running on the PC not just the console. Is there a way of just closing the console but leave all other instances of java running?


Thanks

Wayne

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Closing Java console in a .bat file

#2 Post by foxidrive » 23 Aug 2013 08:25

Add a title to the start command and then close the window with that title.

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Closing Java console in a .bat file

#3 Post by npocmaka_ » 23 Aug 2013 09:05

if you start the java with wmic you'll receive the PID of the java process and you'll be able to kill it by PID

wmic process call create "C:\Program Files\Java\jre6\bin\javacpl.exe"

Waynes
Posts: 2
Joined: 19 Aug 2013 05:14

Re: Closing Java console in a .bat file

#4 Post by Waynes » 27 Aug 2013 07:05

npocmaka_ wrote:if you start the java with wmic you'll receive the PID of the java process and you'll be able to kill it by PID

wmic process call create "C:\Program Files\Java\jre6\bin\javacpl.exe"



This does not work. Even if I get the PID and then use

taskkill /f /fi "pid eq ????"

where ???? is the PID I get the message that no tasks running with the specified criteria.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Closing Java console in a .bat file

#5 Post by foxidrive » 27 Aug 2013 09:12

You must have typo'd something. tasklist returns the same pid for the application - see below:


Code: Select all

z:\>wmic process call create notepad
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ProcessId = 5716;
        ReturnValue = 0;
};


z:\>tasklist |find /i "notepad.exe"
notepad.exe                   5716 Console                    1      4,672 K

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Closing Java console in a .bat file

#6 Post by npocmaka_ » 28 Aug 2013 00:41

Waynes wrote:

This does not work. Even if I get the PID and then use

taskkill /f /fi "pid eq ????"

where ???? is the PID I get the message that no tasks running with the specified criteria.


WMIC might require an administrator permissions... and home editions of windows don't have it :|


You can kill the PID with:

TASKKILL /PID ????

or

TSKILL ???? (may be the easiest way)

or even again with WMIC:
wmic process where PID="????" delete



here's a script that I created a few years ago that starts a process with WMIC and set the produced PID into %ERRORLEVEL% . May be it could be useful for you:

Code: Select all

@echo off
setlocal
 
if [%~1] EQU [] (
        call :echoHelp
        goto :eof
)
 
if [%~1] EQU [-help] (
        call :echoHelp
        goto :eof
)
 
set /A shifter=1
set workdir=.
 
 
:argParser
        if [%~1] EQU [-exec] (
                set exec=%~2
        )
        if [%~1] EQU [-commandline] (
                set commandline=%~2
        )
        if [%~1] EQU [-workdir] (
                set workdir=%~2
        )
        if [%~1] EQU [-host] (
                set host=%~2
        )
        if [%~1] EQU [-user] (
                set user=%~2
        )
        if [%~1] EQU [-pass] (
                set pass=%~2
        )
        if [%~1] EQU [-record] (
                set record=%~2
        )
        shift
        shift
        set /A shifter=%shifter% + 1
       
        if %shifter% EQU 7 (
               
                goto :endArgParser
        )
 
goto :argParser
:endArgParser
 
if "%exec%" EQU "" (
        echo executable not defined
        goto :eof
)
 
if "%host%" NEQ "" (
        set host_param=/NODE:%host%
        if [%user%] NEQ [] (
                set user_param=/USER:%user%
                if [%pass%] NEQ [] (
                        set pass_param=/PASSWORD:%pass%
                )
        )
)
 
if [%record%] NEQ [] (
        set record_param=/RECORD:%record%
)
 
set global_params=%record_param% %host_param% %user_param% %pass_param%
 

 
for /f "usebackq tokens=*" %%G IN (`wmic  %global_params%  process call create "%exec% %commandline%"^,"%workdir%"`)  do (
        echo %%G | find "ProcessId" >nul && (
                for /f  "tokens=2 delims=;= " %%H in ('echo %%G ^| find  "ProcessId"') do (
                        call set /A PID=%%H
                        goto :endLoop1
                )
        )
       
        echo %%G | find "ReturnValue" >nul && (
                for /f  "tokens=2 delims=;= " %%I in ('echo %%G ^| find  "ReturnValue"') do (
                        call set /A RETCOD=%%I
                        goto :endLoop2
                )
        )
       
        call :concat "%%G"     
)
goto :endloop3
 
::successful execution
:endLoop1
if %PID% NEQ 0 (
        echo %PID%
        exit /B %PID%
)
 
::unsuccessful with code
::check the return code and give hints
:endLoop2
 
echo return code : %RETCOD%
 
if %RETCOD% EQU 2 (
        echo -Access Denied
)
 
if %RETCOD% EQU 3 (
        echo -Insufficient Privilege
)
 
if %RETCOD% EQU 8 (
        echo -Unknown failure 
        echo Hint: Check if the executable and workdit exists or if command line parameters are correct.
)
 
if %RETCOD% EQU 9 (
        echo -Path Not Found
        echo Hint: check if  the work exists on the remote machine.
)
 
if %RETCOD% EQU 21 (
        echo -Invalid Parameter
        echo Hint: Check executable path.Check if  host and user are corect.
)
 
exit /b 0
goto :eof
 
::unsuccessful with no code
:endloop3
echo %output%
echo HINT :brackets,quotes or commas in the password may could break the script
goto :eof
 
endlocal
goto :eof
 
 
:echoHelp
        echo %~n0 -exec executubale [-commandline command_line] [ -workdir working_directory] [-host  remote_host [-user user [-pass password]]] [-record path_to_xml_output]
        echo\
        echo localhost cant' be used as in -host variable
        echo Examples:
        echo %~n0  -exec "notepad" -workdir "c:/"  -record "test.xml" -commandline "/A startpid.txt"
        echo %~n0  -exec "cmd" -workdir "c:/"  -record "test.xml" -host remoteHost -user User
        echo\

 
goto :eof
 
:concat
        call set output=%output% %1
 
goto :eof

Post Reply