Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't run main class from JAR file 1

Status
Not open for further replies.

varocho

Programmer
Dec 4, 2000
238
US
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?
 
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
 
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.
 
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.
 
Most likely one of the import statements is not resolved at runtime, and that disables startup.
 
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
 
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
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top