Why does my .jar file throw exceptions, even if my program runs without exception in Eclipse?

I am new to Java and learn how to export applications to .jar files. I created a simple Java application in Eclipse with several tutorial-based classes from Eclipse and Java for all beginners .

When I launch my application through Run -> Run in Eclipse, my application starts without exception. However, when I got to the file -> Export and export my application to a .jar file, and then do

java myLibrary.jar

in my Mac terminal, I get this output from a standard error.

Exception in thread "main" java.lang.NoClassDefFoundError: myLibrary/jar

Caused by: java.lang.ClassNotFoundException: myLibrary.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

What does it mean? How do I skip classes? I tried to check and uncheck the boxes for export. For example, I tried to enable my JUnit tests to no avail.

+2
3

, .

jar cmf MANIFEST.MF myLibrary.jar *

, , :

java -jar myLibrary.jar

MANIFEST.MF :

Main-Class: myLibrary

myLibrary - , .

+1

java , :

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

-jar jar.

, . ( Main-Class). : http://docs.oracle.com/javase/tutorial/deployment/jar/run.html

+1

In eclipse, right-click on your project -> Click on Export -> Java -> Jar file -> Select the source folders (in most cases, the default values ​​should work fine). Specify the location of the jar file. Go to the last page of the wizard, in the "Select the entry point class of the application:" section, select the java class file with the main one.

0
source

All Articles