A well coded batch file is a good solution to run a Java application easily by users.
Here is an example with some comment lines:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
title MyProgram
if "%~1" == "/?" goto ShowHelp
rem Try to find java.exe in current folder or any folder defined in
rem environment variable PATH as Windows command interpreter would do.
set "SearchPath=%CD%;%PATH%"
set "SearchPath=%SearchPath:)=^)%"
set "SearchPath=%SearchPath:\;=;%"
for /F "delims=" %%I in ('echo %SearchPath:;=^&ECHO %') do (
if exist "%%~I\java.exe" (
set "JavaPath=%%~I"
goto RunMyProgram
)
)
echo %~dpn0
echo.
echo Error: Failed to find java.exe in current folder or
echo any folder defined in environment variable PATH.
:GetJavaPath
echo.
echo Please enter path to folder with java.exe.
echo.
echo Enter nothing to exit if Java is not installed at all.
echo.
set "JavaPath="
set /P "JavaPath=Path to java.exe: "
rem Has the user entered a folder path at all?
if "!JavaPath!" == "" goto BatchExit
rem Remove all double quotes from entered folder path.
set "JavaPath=!JavaPath:"=!"
rem Replace all / by \ in entered folder path.
set "JavaPath=!JavaPath:/=\!"
rem If there is a backslash at end, remove this backslash.
if "!JavaPath:~-1!" == "\" set "JavaPath=!JavaPath:~0,-1!"
rem Does java.exe exist in entered folder path?
if exist "!JavaPath!\java.exe" goto RunMyProgram
echo.
echo There is no java.exe in entered folder.
goto GetJavaPath
:ShowHelp
echo For using this application ...
echo.
pause
goto BatchExit
:RunMyProgram
echo.
"%JavaPath%\java.exe" -cp "%~dp0MyProgram.jar" %*
:BatchExit
endlocal
The entire batch code uses its own environment defined with setlocal and endlocal. Read the answer on change directory command cd ..not working in batch file after npm install for a detailed explanation what those two commands do.
On third line the title for the console application is defined which should be edited to whatever you think would be good for your Java application.
The standard parameter to get help on a command or console application on Windows is running it with /?
as parameter. Therefore the batch code checks if first parameter is equal /?
and in this case outputs a help you have to write for your application below label ShowHelp
.
Next the batch file searches in current directory and all directories defined in environment variable PATH
for java.exe
. It is expected that the Java executable could be found automatically and so your Java application can be executed next without any user interaction. This code part is a slightly modified version of the code written by Magoo in his answer on Find the path used by the command line when calling an executable.
But if java.exe
could not be found automatically, the name of the batch file with full path is output to let the user know which application is printing the error message, and next the error message, too.
The user has now the possibility to enter manually or by drag and drop the path to the folder containing java.exe
.
By pressing just RETURN or ENTER the user can exit the batch file in case of recognizing that Java is not installed at all and therefore your Java application can't be used by this user.
Otherwise possible surrounding double quotes are removed from entered folder path, forward slashes are replaced by backslashes as many users use /
instead of \\
on Windows by mistake, and last character of entered folder path is also removed in case of folder path ends with a backslash.
Next is verified if java.exe
really exists in the folder entered by the user.
Note: In case of the user enters for example "\"
the environment variable JavaPath
does not exist anymore on file existence check, but that does not really matter. The entered folder path is referenced using delayed expansion to prevent an exit of the batch file processing because of a syntax error in case of the user made a typing mistake on entering folder path which would result with immediate variable expansion in a syntax error.
It is expected by this batch file that MyProgram.jar
is in the same directory as the batch file itself. %~dp0
references the drive and path of the batch file. This string always ends with a backslash which is the reason why there is no backslash between %~dp0
and MyProgram.jar
.
%~dp0MyProgram.jar
is enclosed in double quotes because the user could have extracted your Java application and the batch file into a folder which name or path contains 1 or even more spaces.
%*
references all arguments (parameters, options) passed to the batch file to forward them to your Java application.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~1
, %~dp0
, %dpn0
and %*
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
title /?