运行java程序出错:java.lang.NoClassDefFoundError
我有以下包结构,我尝试运行示例休眠应用程序
源文件: /home/chander/workspace/HibernateApp/src/com/hib/TranslateSQL.java
我当前的目录:
/home/chander/workspace/HibernateApp/src
编译: $JAVA_HOME/bin/javac com/hib/*.java
--- 成功运行运行
: $JAVA_HOME/bin/java com/hib/TranslateSQL
给出以下异常:
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.URLClassLoader.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.
我设置了类路径和 JAVA_HOME。我似乎没有发现我的命令有什么问题。
还尝试了以下命令:
$JAVA_HOME/bin/java com.hib.TranslateSQL
得到以下异常:
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.
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.URLClassLoader.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指定的类名不正确。而不是这样:
您应该运行:
您应该指定包(
com.hib
)内的类名,而不是文件名< /em> 在目录结构 (com/hib
) 中。编辑:如果这不起作用,则您的类路径可能设置不正确。您可以在命令行中指定它:
You're specifying the classname incorrectly. Instead of this:
you should be running:
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:
我通过这样做让它工作:
I got it to work by doing this: