Error in running java program: java.lang.NoClassDefFoundError
I have the following package structure where im trying to run a sample hibernate app
Source files: /home/chander/workspace/HibernateApp/src/com/hib/TranslateSQL.java
My current directory:
/home/chander/workspace/HibernateApp/src
Compile:
$JAVA_HOME/bin/javac com/hib/*.java
--- This ran successfully
Run:
$JAVA_HOME/bin/java com/hib/TranslateSQL
Giving the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: TranslateSQL (wrong name: com/hib/TranslateSQL)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassL开发者_StackOverflowoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:315)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: TranslateSQL. Program will exit.
I have my classpath and JAVA_HOME set. I dont seem to find what is wrong with my command.
Also tried the following command:
$JAVA_HOME/bin/java com.hib.TranslateSQL
Got the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hib/TranslateSQL
Caused by: java.lang.ClassNotFoundException: com.hib.TranslateSQL
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:315)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: com.hib.TranslateSQL. Program will exit.
You're specifying the classname incorrectly. Instead of this:
$JAVA_HOME/bin/java com/hib/TranslateSQL
you should be running:
$JAVA_HOME/bin/java com.hib.TranslateSQL
You should be specifying a class name which is within a package (com.hib
), rather than a file name in a directory structure (com/hib
.)
EDIT: If that's not working, your classpath may be set up incorrectly. You can specify it on the command line:
$JAVA_HOME/bin/java -cp . com.hib.TranslateSQL
I got it to work by doing this:
cd /home/chander/workspace/HibernateApp/src/com/hib/
export CLASSPATH=$CLASSPATH:/home/chander/workspace/HibernateApp/src/
$JAVA_HOME/bin/java com.hib.TranslateSQL
精彩评论