aspose file tools*
The moose likes Beginning Java and the fly likes Main Class Not Found in JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main Class Not Found in JAR file" Watch "Main Class Not Found in JAR file" New topic
Author

Main Class Not Found in JAR file

Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
Hello all,

I'm attempting to export a JAR file from a package in my Eclipse 3.1 workspace (Windows XP) that contains a class with my main method and for some reason, at runtime I receive an error from the JVM that it cannot find my main class. The class was assigned in the JAR wizard, it is listed in my manifest file, and I've even tried to generate the JAR file from the command line with the same result despite seeing that the main class file was compressed and added to the jar.

Can someone point me in the right direction to correct this? I'm currently running JRE1.5.0_05. Thanks for your help!

Chris Simpkins
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
By jar wizard do you mean the FatJar plugin? I have had no problems with small programs using that.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
No, sorry, I meant the wizard in Eclipse that allows you to export JAR files. It allows you to select the main class.

For some reason, the class is not being found in the JAR file.

-CS
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Can you open your jar file with WinZip and look at its contents? Look at the contents of the manifest file. Is there a correct Main-Class: entry?
Do the jar entries have the correct path for the packages they are in?
Copy and paste results here if further questions.
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
Thanks for your message. So, I took your advice and examined the directory structure in the JAR file with WinZIP. When it is packaged as a JAR, there are three main directories in the JAR file. One is called .settings and contains resources, ui, and core prefs classes that come from eclipse packages. Another folder is called META-INF and contains the manifest file which has been named MANIFEST.MF. The final one is an appropriate directory tree of my classes:

org/pongpong/pong

This directory contains all of the classes that I've coded, including the class that contains my method which is named StartPong.class.

Does the manifest file need to come out of this separate directory so that the virtual machine can determine the appropriate directory path to find the classes?

Also, in looking at this, another question has come up. I've used a number of classes from open source code that were in separate packages in my workspace in the Eclipse IDE. None of the classes from these packages are contained within my JAR. For instance, I am using the httpclient class from apache which is in the package: org.apache.commons.httpclient. This is on my classpath in the IDE and compiles and runs without any problems when I run it from within the IDE; however, will there be problems without these classes in the JAR file at runtime or are the references to the classes compiled into my class files? If the former is true, can you tell me how to do this either in eclipse or using the command line approach. I've made JAR files in eclipse 3.0 without any problems and since I upgraded to 3.1 I've run into this issue. I suspect that some of my settings are wrong.

Any help would be greatly appreciated!

-Chris
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
Sorry didn't answer your question about the Main class entry on the manifest file. It does have a correct path to the main class.
-CS
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Sorry, need more info.
Could you try to execute your jar file with java.exe and copy the full console here. Also post the contents of your manifest file.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Also does the class specified in the manifest Main-Class: entry have a correct main() method?
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
Thanks again for your help Norm. Here is all of the information that you requested:

Manifest file (includes a return after the final line):


The main class (StartPong):


The console output when I try to run the JAR file from the command line:



It appears that despite importing the appropriate l&f package which is on my classpath and recognized by the IDE, the classes are not being packed into the JAR. This gets back to my second question from my post above regarding the remainder of open source packages that I am using in this project. Eclipse recognized all of the imports after I added them to the classpath, but I think that I am going to have the same problem with other package imports as well because when I search through the classes in the JAR file, the only classes that are there are the ones that are included in my package (org.pongpong.pong).

Any thoughts?

Thanks again,
-Chris
[ September 25, 2005: Message edited by: Chris Simpkins ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can I suggest you take a look at the FatJar plugin - it does all this stuff for you.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
When you use the -jar option, Java ignores the global classpath variable. You need to modify your manifest file to include a "Classpath:" (or is it "ClassPath:"?) attribute that indicates where the third-party jar files are located.

Layne


Java API Documentation
The Java Tutorial
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
Thank you very much for pointing out the FatJar plugin Barry. I'll definitely check it out.

I'm still interested in learning more about these classpath, jar packaging issues. I've read a number of resources both on the web and in print and in my experience this information hasn't been very well documented. Perhaps I haven't looked in the right places I'll post a message to let you know how the FatJar plugin worked.

Layne, do you mean something like this?


My eclipse workspace is where I have all of the JAR's and this is the path to that directory. Can you list multiple directories if the imported packages are in different locations or do they all have to be in the same directory?

Thanks for all of your feedback. This has been extremely helpful.

-Chris

[ September 25, 2005: Message edited by: Chris Simpkins ]

[ September 25, 2005: Message edited by: Chris Simpkins ]
[ September 25, 2005: Message edited by: Chris Simpkins ]
Raistlin Zischer
Greenhorn

Joined: Jul 05, 2005
Posts: 25
I have experienced a similar problem after changing to 3.1, but I updated my WinXP environment variables to include CLASSPATH. I have this variable setup to point to .;..; and the location of the lib folder I am using. This I've done this I've not had any of the similar problems you have been having.

Hopes this helps some.
Chris Simpkins
Greenhorn

Joined: Sep 23, 2005
Posts: 12
I figured out what was causing the problem. One of the JAR files (substance look and feel) that my application used was being included in the main directory of my application's JAR without the path structure that was being used for the import. I downloaded the substance L&F JAR as compiled files and placed the JAR on the buildpath for my own classes. Interestingly, this worked when run from within Eclipse, but obviously, previously compiled files are not re-compiled and the substance L&F JAR was inaccessible in the location where it was placed in my JAR.

My solution was to download the source files and include them in a separate project in my workspace. I then listed this as one of the dependent projects on my buildpath and voila... it worked. Proper file structure and a JAR of Pong!

Thanks to all who helped. If anyone runs into a similar problem and needs any help, feel free to e-mail. The Eclipse IDE makes jar file creation relatively easy, but you have to play around with your settings a bit.

By the way, the FatJar plugin for Eclipse appears to be another option. I didn't use it because this worked but you can get more information here: http://fjep.sourceforge.net/

-Chris
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Chris Simpkins:
...Layne, do you mean something like this?


...


Almost. For one thing, I think you need to enclose this path in "s since there is a space character in one of the folder names. Secondly, this indicates that Java can search the directory structure under the workspace folder for .class files. This isn't quite what you want. When adding JAR files to the classpath, you always need to include the full path to the JAR file, including its name. This is the same no matter how you specify the classpath for a program, whether that be by using the -cp option, the CLASSPATH system var, or the ClassPath: manifest attribute, you need to include any JAR files individually. So if you have a JAR file named "mypackage.jar" in a folder named "C:\java", you need the following:


HTH

Layne
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Main Class Not Found in JAR file