INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can't run main class from JAR file

Can't run main class from JAR file

(OP)
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?

RE: Can't run main class from JAR file

Hi,

Would it be possible to see the javaCmdOutput.txt if it does not contain sensitive info?

Other than that, I'd be keen to know if I can execute the code outside the jar. i.e. after you have unjar'd the file, is it possible to modify the classpath according the the Class-Path line of the manifest and do a:

CODE

java com.mycompany.package1.MyClass

Cheers,
Scott

RE: Can't run main class from JAR file

(OP)
After unjarring the file, I ran this command to run the main() method of the specified Main-Class:

java -cp .;lib\JARfile1.jar;lib\JARfile2.jar com.mycompany.package1.MyClass

and still got this error message:

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

Even updating the classpath command-line value to include the relative path of the MyClass.class file didn't help.

RE: Can't run main class from JAR file

(OP)
With respect to the javaCmdOutput.txt file, I see lines like:

[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]

and similar lines for only 5 classes from the 'com' package, including com.mycompany.package1.MyClass. There's a lot more than 5 classes within the 'com' subpackages.

RE: Can't run main class from JAR file

Most likely one of the import statements is not resolved at runtime, and that disables startup.

RE: Can't run main class from JAR file

Hi again,

Don't worry about the lack of [Loaded ...] messages.  They only show when an object is instantiated in the code.

I can see the error you are getting in the java.c source code:

CODE


       mainClass = LoadClass(env, classname);
        if(mainClass == NULL) { /* exception occured */
            const char * format = "Could not find the main class: %s. Program will exit.";
            ReportExceptionDescription(env);
            message = (char *)JLI_MemAlloc((strlen(format) +
                                            strlen(classname)) * sizeof(char) );
            messageDest = JNI_TRUE;
            sprintf(message, format, classname);
            goto leave;
        }

problem is I cannot get any of my JVM's to kick out that precise error.  They all give something much more useful to explain what the problem is.  Since you can see the [Loaded com.mycompany.package1.MyClass ..], I suspect the problem is an unexpected error after loading that is causing the LoadClass to return NULL.

Can you do a

CODE

java -version

for me?

I assume you are on windows due to the semicolon ';' classpath delimiter.

I'll be honest - I don't know what the problem is due to.  But if you are happy to follow the investigation path, I am happy to help.

Cheers,
Scott
 

RE: Can't run main class from JAR file

Forgot one thing, can you also give us the output from the following if it has no sensitive info:

CODE

javap -classpath . -verbose com.mycompany.package1.MyClass

Cheers,
Scott

RE: Can't run main class from JAR file

Sorry for being obvious but ... are you sure the class is in the correct folder structure with the same name and capitalization? Are you sure the file is not corrupted and the java compilation and execution versions are the same?

Cheers,
Dian

RE: Can't run main class from JAR file

(OP)
The other developer I was working with figured it out, and it was as TonHu mentioned, one of the import statements wasn't resolved at runtime. To be a little more specific, a 'LogFactory' class that was referenced in the parent class of 'MyClass' didn't actually exist within the JAR file for logging that we had been using. Once we switched to the appropriate JAR file, the problem was resolved.

Thanks to all who took the time to respond, and I apologize for not updating this thread sooner.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close