How to run a jar file in a linux commandline

Java

Java Problem Overview


How to set the classpath to the current directory and also run the jar file named load.jar present in the current directory by providing the argument as load=2 from a linux command line.

I did try to run the jar as follows but its executing classes from some other directory.

java -cp ./load.jar:$CLASSPATH load.Start load=2

Java Solutions


Solution 1 - Java

Running a from class inside your JAR file load.jar is possible via

java -jar load.jar

When doing so, you have to define the application entry point. Usually this is done by providing a manifest file that contains the Main-Class tag. For documentation and examples have a look at this page. The argument load=2 can be supplied like in a normal Java applications:

java -jar load.jar load=2

Having also the current directory contained in the classpath, required to also make use of the Class-Path tag. See here for more information.

Solution 2 - Java

For example to execute from terminal (Ubuntu Linux) or even (Windows console) a java file called filex.jar use this command:

java -jar filex.jar

The file will execute in terminal.

Solution 3 - Java

Under linux there's a package called binfmt-support that allows you to run directly your jar without typing java -jar:

sudo apt-get install binfmt-support
chmod u+x my-jar.jar
./my-jar.jar # there you go!

Solution 4 - Java

For OpenSuse Linux, One can simply install the java-binfmt package in the zypper repository as shown below:

sudo zypper in java-binfmt-misc
chmod 755 file.jar
./file.jar

Solution 5 - Java

In my case I had to utilize an additional flag- console to get it up and running:

java -jar jarfilename.jar -console

The console flag was needed to run the file in shell and do instructions needed for setup like installation path and accept terms and conditions.

Solution 6 - Java

sudo -sH
java -jar filename.jar

Keep in mind to never run executable file in as root.

Solution 7 - Java

copy your file in linux Java directory

cp yourfile.jar /java/bin

open the directory

cd /java/bin

and execute your file

./java -jar yourfile.jar

or all in one try this command:

/java/bin/java -jar jarfilefolder/jarfile.jar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDark MatterView Question on Stackoverflow
Solution 1 - JavaStefan FreitagView Answer on Stackoverflow
Solution 2 - JavaAshraf SadaView Answer on Stackoverflow
Solution 3 - JavaBruno BiethView Answer on Stackoverflow
Solution 4 - JavaMagnus MelwinView Answer on Stackoverflow
Solution 5 - JavaashwinjosephView Answer on Stackoverflow
Solution 6 - JavaAaditya PuraniView Answer on Stackoverflow
Solution 7 - JavaPaulView Answer on Stackoverflow