Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAR file problem

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the file "MainClass.class" in the directory "com/test" inside the jar?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try opening the jar file (winzip, winrar would work) and check if the MainClass.class actually exists?
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic