How do I run a .JAR file via the terminal

21,946

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.

Share:
21,946
Jaiden Fahey
Author by

Jaiden Fahey

if its not far then why not walk

Updated on September 18, 2022

Comments

  • Jaiden Fahey
    Jaiden Fahey over 1 year

    Mispelled classname in manifest.txt file caused this error

    enter image description hereenter image description hereIm 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
      AliN about 11 years
      I couldn’t get your question. Do you mean a command to open a jar file? It’s java -jar path-to-file.
    • Jaiden Fahey
      Jaiden Fahey about 11 years
      yeah Im trying to export a .jar file to the terminal line
    • Jaiden Fahey
      Jaiden Fahey about 11 years
      lightning@rigel5:~$ java /home/lighting/Documents/temp.jar Error: Could not find or load main class .home.lighting.Documents.temp.jar lightning@rigel5:~$
    • Jaiden Fahey
      Jaiden Fahey about 11 years
      at least I thought I did. how do I do that?
  • Jaiden Fahey
    Jaiden Fahey about 11 years
    'clearly' never sounded better. will have a look :D
  • Jaiden Fahey
    Jaiden Fahey about 11 years
    when I create the manifest file where do I put it?
  • H.-Dirk Schmitt
    H.-Dirk Schmitt about 11 years
    see the -m option for the jar command
  • Jaiden Fahey
    Jaiden Fahey about 11 years
    lightning@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
    Jaiden Fahey about 11 years
    See 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
    Jaiden Fahey about 11 years
    lightning@rigel5:~$ java -jar MyFirstClass.jar Error: Unable to access jarfile MyFirstClass.jar lightning@rigel5:~$
  • Jaiden Fahey
    Jaiden Fahey about 11 years
    this 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
    Jaiden Fahey about 11 years
    lightning@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
    Muhammad Sholihin about 11 years
    it 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
    Jaiden Fahey about 11 years
    I had mispelled the classname in the manifest file. the classname is MyFirstClass not 'MyFirstCLass'. Oh, so embarrassed