Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a jar file in a linux commandline

Tags:

java

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 
like image 937
Dark Matter Avatar asked Nov 11 '13 11:11

Dark Matter


People also ask

Can Linux open jar files?

How To Run A JAR File In Linux. If you have Java installed on your Linux, you will be able to run the JAR file with a double-click or picking option from right-click. You only need Java Runtime Environment and you will be able to easily run the file in Linux.

What is jar command in Linux?

Description. The jar command is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. However, the jar command was designed mainly to package Java applets or applications into a single archive.


2 Answers

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.

like image 101
Stefan Freitag Avatar answered Oct 04 '22 05:10

Stefan Freitag


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.

like image 24
Ashraf Sada Avatar answered Oct 04 '22 05:10

Ashraf Sada