RSS

Tip: How to directly run java classes files that reside in jar files without using “-jar” command

21 Sep

We all know that jar files are archives for java classes, libs, and other execution-depended files, e.g. images. Usually jar files are independent, that is to say, we may run them using command “>$ java –jar filename”. However, for some research purposes, we need to extract raw class files from a jar file to analyze and execute. At first, I thought it’s a piece of cake. Open a jar file and extract all files out. Find the entrance class(which contains static main method, usually will be indicated in the manifest files), and input “>$ java entrance_filename”. Yet everything doesn’t go as smooth as I expected. I kept encountering “NoClassDefFoundError” error. I searched the internet and found this error indicates that class loader can’t find classes which are available during the compiling time. Causes to this problem vary in many different ways. Below are two possible causes when we try to directly run classes files that reside in jar files.

Forget to check manifest files to include necessary classpath.

“NoClassDefFoundError” error sometimes can be a real error. Be sure to check manifest files included in the jar files. Sometimes manifest files will has information of extra class path information to support execution of this jar file. We need to also include those class paths when execute raw class files. If you miss this part. There is no doubt some classes’ definition are really missing. So be sure to include all class paths if manifest files indicate any.

Execute the entrance class in a wrong package level.

For a large project, we may have many sub-folders in a jar file. And our classes are most likely defined in different packages, which in most cases correspond to different sub-folder levels. After checking and including all the possible class paths, I still got this “NoClassDefFoundError” error every time. And I searched for many hours and tried a lot of solutions. None of them works. When I was staring at the error message with frustration, (the message is like this, “XXX NoClassDefFoundError: (XXX/entrance) wrong name…” that indicated that even the entrance class can not be found properly), I suddenly realized that there is a prefix “XXX/” before entrance class name. This message doesn’t say entrance class is a wrong name, but “XXX/entrance” is a wrong name. I “cd” back to higher folder level and re-type the “java XXX/entrance” command. Then everything works like a charm. To sum up, my experience shows that we should execute the classes at a right folder level that usually corresponds to the class package level.

Conclusion

Jar files actually contain no magic. They are merely collections of necessary files. As a result, it is absolutely feasible to extract all files and execute them by raw “java” command if we configure the command correctly. Smile

Advertisements
 
Leave a comment

Posted by on September 21, 2012 in Technical notes

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: