How do I run a .JAR file via the terminal
Solution 1
You can run java package using this command on your terminal:
java -jar MyFirstClass.jar
If you don't have Java Runtime Edition (JRE) you'll need to install default-jre
package.
Solution 2
First you need a main-method in your class file - public static void main(String args[]){ ... } - probably done...
In the jar-file you need a manifest.txt file - inside the class with the main method has to be noted like this -> Main-Class: MyPackage.MyClass
To create one you can use the commandline tool jar ...
If you don't have an manifest-file in your jar, you can just type:
java -cp jarFileName.jar ClassNameWithMainMethod
Solution 3
java /home/lighting/Documents/temp.jar Error: Could not find or load main class .home.lighting.Documents.temp.jar lightning
This shows that you clearly have done a mistake in the META-INF/MANIFEST.MF
See this Tutorial for more informations.
Comments
-
Jaiden Fahey over 1 year
Mispelled classname in manifest.txt file caused this error
Im trying to run a java file outside of my IDE by exporting the .jar file to my documents folder and trying to display the text 'hello world'in the file on the terminal line. I attempted to create a Manifest.txt file with this in it,
Main-Class: lightning.java.hello.MyFirstClass
I then used this script to create a file MyJar.jar and got this back,
lightning@rigel5:~$ jar cfm MyJar.jar Manifest.txt lightning.java.hello/*.class java.io.FileNotFoundException: Manifest.txt (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:97) at sun.tools.jar.Main.run(Main.java:171) at sun.tools.jar.Main.main(Main.java:1177)
-
AliN about 11 yearsI couldn’t get your question. Do you mean a command to open a jar file? It’s
java -jar path-to-file
. -
Jaiden Fahey about 11 yearsyeah Im trying to export a .jar file to the terminal line
-
Jaiden Fahey about 11 yearslightning@rigel5:~$ java /home/lighting/Documents/temp.jar Error: Could not find or load main class .home.lighting.Documents.temp.jar lightning@rigel5:~$
-
Jaiden Fahey about 11 yearsat least I thought I did. how do I do that?
-
-
Jaiden Fahey about 11 years'clearly' never sounded better. will have a look :D
-
Jaiden Fahey about 11 yearswhen I create the manifest file where do I put it?
-
H.-Dirk Schmitt about 11 yearssee the
-m
option for thejar
command -
Jaiden Fahey about 11 yearslightning@rigel5:~$ java -cp temp.jar.lightning.java.hello.MyFirstClass I typed this in and got a java command usage index and did not display the file
-
Jaiden Fahey about 11 yearsSee oracle.com/technetwork/java/javase/documentation/index.html for more details. lightning@rigel5:~$ java -cp temp.jar MyFirstClass.lightning.java.hello Error: Could not find or load main class MyFirstClass.lightning.java.hello lightning@rigel5:~$
-
Jaiden Fahey about 11 yearslightning@rigel5:~$ java -jar MyFirstClass.jar Error: Unable to access jarfile MyFirstClass.jar lightning@rigel5:~$
-
Jaiden Fahey about 11 yearsthis is my MANIFEST.MN file;Manifest-Version: 1.0 Class-Path: . Main-Class: lightning.java.hello.MyFirstClass I created a file Manifest.txt and it looks like this;MainClass: lightning.java.hello.MyFirstCLass
-
Jaiden Fahey about 11 yearslightning@rigel5:~$ java -jar MyFirstClass.jar Error: Unable to access jarfile MyFirstClass.jar lightning@rigel5:~$ temp.jar lightning.java.hello.MyFirstClass temp.jar: command not found
-
Muhammad Sholihin about 11 yearsit seems you are on the wrong path, because it can not find the jar file. make sure you are in the jar file directory when using the command.
-
Jaiden Fahey about 11 yearsI had mispelled the classname in the manifest file. the classname is MyFirstClass not 'MyFirstCLass'. Oh, so embarrassed