Running a Jar File Without Directly Calling 'Java'

Running a JAR file without directly calling `java`

See Documentation/java.txt in the Linux Kernel documentation, which tells you how to configure a system using the binfmt_misc kernel module to run Jar files automatically. However, this is a configuration option you change on a computer, not something you change about the jar file, so it doesn't follow the jar file from system to system.

Can run a jar file without knowing the jar file name

Try the below approach for executing script without mentioning the jar's release version.

 #!/bin/bash

PROJECT_HOME=/opt/services/testing-batchp
PROJECT_JAR=batch-*.*.jar
[ -f /etc/environment ] && . /etc/environment

nohup java -jar -Dspring.config.location=${PROJECT_HOME}/config/ ${PROJECT_HOME}/${PROJECT_JAR} $1 $2 >/dev/null 2>&1 &

Make sure that only one valid jar is there in the deployment folder.

How can I execute .jar file from the terminal without specifying its name

I am not sure it would be a good idea to just run everything in a directory, but you could:

FOR %A IN ("*.jar") DO (java -jar "%~A")

Execute .JAR by name without specifying 'java -jar' command options

You can't really avoid that easily.

A common strategy is to deploy an executable startup script with your application; one per platform since this is platform specific (e.g. an executable shell script for Linux, and a batch script for Windows). Standard shell scripting techniques can be used to pass command line parameters to the script on to your application.

Note that there are some clever platform-specific tricks, such as the Linux one here (and I'm not certain but it may be possible to make all jars executable on the command line with some dirty registry hacks on Windows); but if you are going for platform-independence (and you don't want to have to post-process your JARs after a build), deploying a wrapper script is the way to go.

Can an executable .jar file be called without having to use its full path?

You can add a variable to hold the directory:

export JARDIR=/some/path/to
java -jar $JARDIR/thearchive.jar

I'm not sure you can do it from environment variables implicitly.

Run a JAR file from the command line and specify classpath

When you specify -jar then the -cp parameter will be ignored.

From the documentation:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

You have two options:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp:

    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

suggestion to run jar files in practical way

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 /?


Related Topics



Leave a reply



Submit