1

I'm new to JAVA and trying unable to figure out what's wrong with the project I created.

I created a JAVA project in eclipse and exported a jar (not runnable jar). I unchecked .project and .classpath file options while exporting. Now I'm trying to run that jar and getting NoClassDefFoundError:

I'm pasting the command and the errors below:

C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar -jar D:\Web\HbaseGateway\bin\HBaseGateway.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
        at hbase.gateway.HBaseGatewayEntryPoint.main(HBaseGatewayEntryPoint.java
:22)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Now, interesting thing is I'm able to run the main class using the following command. In the below command I removed the -jar switch and passed the main class to java.exe.

C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar hbase.gateway.HBaseGatewayEntryPoint
Gateway Server Started

Why NoClassDefFoundError is being thrown? Am I missing something here?

Mayank
  • 5,454
  • 9
  • 37
  • 60

2 Answers2

4

For -jar option to work, you must add Main-class key in manifest. Refer the java command documentation http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html

-jar option

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.

Jaydeep Rajput
  • 3,605
  • 17
  • 35
  • I do have manifest file with the main class as `hbase.gateway.HBaseGatewayEntryPoint`... But I don't have jdk installed. Does it matter? – Mayank Apr 13 '13 at 12:30
  • Ideally should not. Try to run java command without any options.Check if the help shows -jar option or not. – Jaydeep Rajput Apr 13 '13 at 12:35
  • Since the error comes from within the main class, it is not a `Main-Class` issue, as the OP also states. – NilsH Apr 13 '13 at 13:50
0

When you use the -jar option, the classpath is read from the MANIFEST.mf file as well. From the docs for the -jar option:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

This means that you have to add all your dependencies in a Class-Path section of the manifest file.

NilsH
  • 13,705
  • 4
  • 41
  • 59