This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JAR file problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAR file problem" Watch "JAR file problem" New topic
Author

JAR file problem

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear friends,


I created a JAR file from Eclipse IDE. The classes in the JAR contains applet and Swing code. The usual basic problem is happening when i double click the JAR file,
"Could not find the main class. Program will exit".

I also tried this command: java -jar MyJar.jar.
Following error occurred:
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/MainClass

Following are the things i ensured:
(1) System's environment variable PATH has this value: C:\Program Files\Java\jdk1.5.0_12.
(2) I opened the MANIFEST.MF file and added the Main-Class and Class-Path attributes
(3) Entered a carriage return at the end of the MANIFEST.MF file.
(4) I also added the JRE path to the PATH environment variable.

But still the problem exists!!

Kindly provide your valuable help

Thanks,
Vijay
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Is the file "MainClass.class" in the directory "com/test" inside the jar?


[Jess in Action][AskingGoodQuestions]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3970
    
  17

It looks like the Main Class is in the package com.test. Does have the following line of code at the top of the file:


The file should also be packaged inside the JAR, so that it will be found in:
MyJar.jar!/com/test/MainClass.class

The Main-Class attribute in the Manifest should be:
Main-Class: com.test.MainClass

If all else fails, make sure you read:
The JAR Tutorial


Steve
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear Ernest & Steve,

Thank you very much for the reply.

I double checked the above mentioned.

The Main-Class attribute in the META-INF/MANIFEST.MF given contains the fully qualified name com.test.MainClass
The main class is packaged properly with the com/test/ package structure.
The main class contains the package com.test; statement.
Although i exported the JAR file from Eclipse IDE, i ensured that all the classes are properly packaged into the JAR with the correct package structure.

Still the same problem!

Thanks,
Vijay
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
I just exported a jar file from eclipse. Try this. Create manifest.txt (must be that syntax) in the root folder with:

Manifest-Version: 1.0
Main-Class: com.test.MainClass

Then export the jar file, and it should find the manifest. When I tried creating manifest.mf or Manifest.mf it wouldn't find the mainfest file.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

If you are using Eclipse, you can let eclipse create the manifest file for you.
At the tail end of the wizard (project->export->jar), depending on version, you will be asked if Eclipse should create a manifest file. If you select that option, you will be asked for a main class. Try using eclipse to build the manifest file for you.
If you are able to get the jar running by this approach, compare the eclipse created manifest.mf and your manifest file to figure out what you did wrong.

@Ryan
Does your packaged jar contain the manifest.txt (only) in the META-INF directory and still work?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear all,

Thanks for spending your valuable time for solving my issue.

But unfortunately, i am still having that problem.

I created the JAR by export option from Eclipse. The MANIFEST.MF file was generated by Eclipse only. I didn't specify any particular file.
JAR file is searching only for Manifest.mf it doesn't understand manifest.txt!
Next thing i tried is creating the JAR file using jar command. I used the following command:
The com\ directory contains all the required class files to be packaged.

jar -cfm MyJar.jar C:\Manifest.txt com\

The Manifest.txt has the following entries:
Main-Class: com.test.MainClass
Class-Path: C:\Log4j.jar

The above entries are inserted into the Manifest.mf file created by the jar command.

I ensured that the classes in the JAR file contain the correct package structure.

On executing, java -jar MyJar.jar it gives:
Exception in thread "main" java.lang.NoClassDefFoundError Could not find main class com/test/MainClass

Confused!!

Thanks,
Vijay
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

Can you try opening the jar file (winzip, winrar would work) and check if the MainClass.class actually exists?
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Hi Maneesh,

I am using Winzip to check all the classes are properly placed, and if placed, the classes are in the correct package structure.

All the classes are wrapped up perfectly into the JAR file including the Main class, but still the same problem continues!


Thanks,
Vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAR file problem
 
Similar Threads
Hi , Iam ananya new to java tech,
Running Java from jar file!
Connector/J driver error
setting JAVA_HOME Explicitly
jar runs on desktop but not laptop