wood burning stoves 2.0*
The moose likes Java in General and the fly likes Can't run main class from JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic
Author

Can't run main class from JAR file

Vance Arocho
Greenhorn

Joined: Sep 29, 2011
Posts: 6
I'm trying to run the main() method of a class contained within a JAR file. Here's the error message I get:

Exception in thread "main" Could not find the main class: com.mycompany.package1.MyClass. Program will exit.

Here's what the manifest file looks like:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: some_user
Build-Jdk: 1.6.0_26
Package: com.mycompany.package1
Main-Class: com.mycompany.package1.MyClass
Class-Path: lib/JARfile1.jar lib/JARfile2.jar ...

Just to be clear, there are a number of JAR files contained within the 'Class-Path' value in the manifest file. And there is a blank line at the end of the manifest file. I mention this because I've seen that this has been an issue for others.

I've 'unjar-ed' the original JAR file, here is the directory structure:

- 'com' directory - this folder contains subfolders containing .class files, one of which has been specified as the 'Main-Class' within the manifest file
- 'lib' directory - this folder contains multiple required JAR files
- 'META-INF' directory - this folder contains the manifest file, as well as 2 subfolders
- 'org' directory - this folder contains subfolders containing .class files
- multiple XML files in support of the Spring framework
- several .properties files

When I run this command

java -jar -verbose myjarfile.jar > javaCmdOutput.txt

to get verbose messages re-directed to a text file, I see that the specified 'Main-Class' does get loaded, but one of the classes (from the 'com' package) it imports does not.

Any ideas as to what I can try next to resolve this problem?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to the Ranch.

For this to work, you must have the Main-Class attribute set correctly, and there must be a file /com/mycompany/package1/MyClass.class inside the JAR file (in the right directory).

Note that the "lib" directory with JAR files inside your JAR file is not going to work. Java cannot load JAR files inside JAR files. You must keep those other JAR files outside of your own JAR file. If your program needs classes from those nested JAR files, then this is most likely the cause that you get problems.

For a detailed tutorial on how to work with executable JAR files, see: Packaging Programs in JAR Files from Oracle's Java Tutorials.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vance Arocho
Greenhorn

Joined: Sep 29, 2011
Posts: 6
Thanks, Jesper. Just a quick clarification, the 'lib' directory was added to the directory containing the 'unjarred' JAR file after the fact, in order to resolve error messages I was getting about missing classes belonging to the other JAR files.

When I run this command

java -jar -verbose myjarfile.jar > javaCmdOutput.txt

to get verbose messages re-directed to a text file, I see that the 'Main-Class' specified within the manifest file does get loaded, but one of the classes (from the 'com' package) it imports does not.

Any ideas as to what I can try next to resolve this problem?


Jesper de Jong wrote:Welcome to the Ranch.

For this to work, you must have the Main-Class attribute set correctly, and there must be a file /com/mycompany/package1/MyClass.class inside the JAR file (in the right directory).

Note that the "lib" directory with JAR files inside your JAR file is not going to work. Java cannot load JAR files inside JAR files. You must keep those other JAR files outside of your own JAR file. If your program needs classes from those nested JAR files, then this is most likely the cause that you get problems.

For a detailed tutorial on how to work with executable JAR files, see: Packaging Programs in JAR Files from Oracle's Java Tutorials.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If the Class-Path attribute looks like this in the manifest file:

Class-Path: lib/JARfile1.jar lib/JARfile2.jar

then Java expects that in the directory on filesystem where your JAR file is, there is a lib directory which contains those other JAR files. In other words, suppose that your JAR file is in C:\MyProject, then you should have these files:

C:\MyProject\myjarfile.jar
C:\MyProject\lib\JARfile1.jar
C:\MyProject\lib\JARfile2.jar

Otherwise Java is not going to be able to find JARfile1.jar and JARfile2.jar and you'll get NoClassDefFoundErrors if you're trying to use a class that's inside one of those JAR files.

Also, Java is not going to know that it has to look in the "lib" directory inside the JAR file for those classes. So even if you put the unjarred JAR files in the "lib" directory, Java will still not be able to find the classes there. If you don't want those separate JARfile1.jar and JARfile2.jar outside of your own JAR file, then you'll need to unjar those and put the contents in the root of your own JAR file; not in the "lib" directory.

If your goal is to package your program in a single JAR file, then a tool such as One-JAR might be useful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't run main class from JAR file
 
Similar Threads
How do i find out the relative path for my jar?
Problem updating the JAR file
Jar issue - NoClassDefFoundError when attempting to run. Class-Path set in MANIFEST.MF
Regarding Jar File
problem with classpath