65

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
Dark Matter
  • 2,231
  • 3
  • 17
  • 31
  • 2
    Can you show us what you have tried so far? I also suggest to read the command page of `java`. – Stefan Freitag Nov 11 '13 at 11:09
  • 3
    read the `jar` command man page? You mean the `java` command man page, right? An RTFM response should point to the right FM. – rolfl Nov 11 '13 at 11:11

7 Answers7

138

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.

Akshay Arora
  • 729
  • 1
  • 8
  • 20
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • Thanks. `Failed to load Main-Class manifest attribute from load.jar`. How can I solve this error. – Dark Matter Nov 11 '13 at 11:14
  • Your jar missed the Manifest file. Have a look here: [http://stackoverflow.com/questions/2591516/why-has-it-failed-to-load-main-class-manifest-attribute-from-a-jar-file](http://stackoverflow.com/questions/2591516/why-has-it-failed-to-load-main-class-manifest-attribute-from-a-jar-file) – Stefan Freitag Nov 11 '13 at 11:24
  • I ket the manifest.txt in the same directory where my classes are and it had the value `Main-Class: load.sampLoad` and I issed the command `jar cfm load.jar manifest.txt *` and the jar file is created with an additional directory `Meta-INF` with the filename `MANIFEST.MF` but it does not have the line which I had in manifest.txt but has only the java version used to compile. What went wrong here. – Dark Matter Nov 11 '13 at 11:58
  • i was stuck in this command for a whole day, only to realize that i haven't put the - sign before jar when executing this command :/ – Dilini Peiris Nov 27 '20 at 16:01
21

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.

Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
9

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!
Bruno Bieth
  • 2,317
  • 20
  • 31
  • Doesn't work for me: "invalid file (bad magic number): Exec format error" – A. Kojen Jul 04 '19 at 14:52
  • @A.Kojen Try `sudo apt-get install jarwrapper`, which is a package on top of `binfmt-support`. At least for me, the default for `binfmt-support` was `jexec` -- which I think is OpenJDK's -- and that couldn't run my .jar for some reason. – Michael Krebs May 20 '20 at 12:24
0

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
Magnus Melwin
  • 1,509
  • 1
  • 21
  • 32
0

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.

ashwinjoseph
  • 359
  • 3
  • 12
-2
sudo -sH
java -jar filename.jar

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

spongebob
  • 8,370
  • 15
  • 50
  • 83
-5

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

Paul
  • 1
  • 3