Jesse Brands
Jesse Brands

Reputation: 2877

Creating a .JAR file fails

I'm using Eclipse, and I have a pretty simple project, I'll save you the code. It executes just fine and runs just fine. It has two .java files, in fact, the project tree is like this:

**mewLog**  
 - src/ (dir)   
   mewlog/ (package)  
    - MLGlobal.java  
    - MLMain.java (surprise, this one contains main and some initialization methods)    
   mlGUI/ (package)  
     - this one is empty (empty package!)  

So, I go to File -> Export. I follow the steps as per usual and it makes my .JAR file just fine without any warnings or issues. Good!

So far so good one would say, so I rush up to my Terminal, navigate to the folder I exported my jar in, and execute it as follows:

java -jar mewLog_macosx-x86.jar

BAM! Here it goes wrong, the terminal gives me the following output:

Exception in thread "main" java.lang.NoClassDefFoundError: MLMain
Caused by: java.lang.ClassNotFoundException: MLMain
    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:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

Ok. Before you say "Oh hurr durr you gotta add a class path". I tried, it doesn't work, just gives me "BLAH" no matter what I do.

I'm using Mac OS X Snow Leopard.

Output from jar -tf mewLog_MacOSX-x86.jar

jar -tf mewLog_MacOSX-x86.jar  
META-INF/MANIFEST.MF  
.project  
.classpath  
mewlog/MLGlobal.class  
mewlog/MLMain.class

Upvotes: 1

Views: 1838

Answers (6)

Doon
Doon

Reputation: 20232

does

java -cp mewLog_macosx-x86.jar mewlog.MLMain

work?

if so then your manifest file in the Jar is pointing to the wrong Main-Class (Not taking the package into account)

Upvotes: 2

OscarRyz
OscarRyz

Reputation: 199195

You have to put compiled class in the jar and specify in the manifest file which is the main class.

Try this in the terminal:

$cat Hola.java
package hola;

class Hola {
        public static void main( String ... args ) {
                System.out.println("Hola mundo!");
        }
}

$cat  m.mf
Main-Class: hola.Hola

$javac -d . Hola.java


$jar -cmf m.mf hola.jar hola

$java -jar hola.jar
Hola mundo!

Then compare the content of your jar and see what's different ( beside the obvious different class names )

The point here is to identify if the structure of your jar file is correct or not ( providing the output of jar -tf mewLog_macosx-x86.jar would help too )

I hope this helps.

Upvotes: 0

simon
simon

Reputation: 12902

I would suggest

  1. creating the JAR file from command line with java -jar and familiarizing with the build process. See http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

  2. installing the Fatjar plugin for Eclipse from http://fjep.sourceforge.net/

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346240

Your problem is that the main class needs to be listed with its fully qualified name, i.e. inclding the package. Java is looking for the class MLMain, which means it looks directly in the class path root. But your class is mewlog.MLMain, inside the mewlog package.

Upvotes: 1

babernathy
babernathy

Reputation: 803

Dumb question, but did you choose "File->Export->Java->JAR file" or "File->Export->Java->Runnable JAR file"? A jar is not by default runnable unless the Manifest.MF file tells the VM what the main class is.

Upvotes: 0

duffymo
duffymo

Reputation: 308733

First of all, .java files don't go into JAR files - .class file do. The JVM can't execute .java files. You have to compile them first.

Second, you have to add a manifest specifying the class that has the main method if you intend to make this an executable JAR.

Best to leave Eclipse out of it and do it by hand the first time. Use the jar.exe tool in the /bin directory.

Here's a link that may (or may not) help you.

Upvotes: 1

Related Questions