0

I have a jar file to run but I want to run in more practical way without mention "java" or "java -cp blah blah"

Currently, I need to run my jar file using "java -cp \MyProgram.jar [arguments]

However,I don't want to run my program using such command. I think I need to create batch file or command file in which call my java program? or is there any other more proficient way to run jar file?

Thanks

goutthee
  • 329
  • 4
  • 14
  • Running a Java process without calling `java` somewhere sounds not possible to me. – Tim Biegeleisen Oct 31 '16 at 05:59
  • It is possible if you set the default program for opening a jar file to java or a script at the OS level. But this is not portable to other machines where you have to continue the same configuration. – Sivaswami Jeganathan Oct 31 '16 at 06:02
  • Yes create a batch file or command file. Or you make a runnable JAR file so that you can double-click on it. http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file – Erwin Bolwidt Oct 31 '16 at 06:03
  • @TimBiegeleisen I mean calling java from bat file because I find it's not proficient to run program using "java -cp blah blah blah". – goutthee Oct 31 '16 at 06:03
  • @ErwinBolwidt since I have to specify some arguments, I think I can't use runnable jar file? thanks I prefer using such command like "myprogram.bat -arg1 [arg1value] -arg2 [arg2value]" However, I would like to find out how people find running program between using "java -jar or java-cp blah blah" and "program.bat -arguments" – goutthee Oct 31 '16 at 06:06
  • In case of runnable JAR (openwith configured at OS level), you might need to specify the main class in Jar manifest – Sivaswami Jeganathan Oct 31 '16 at 06:06

1 Answers1

0

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 /?
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143