运行java程序出错:java.lang.NoClassDefFoundError

发布于 2024-12-08 09:59:42 字数 2245 浏览 2 评论 0原文

我有以下包结构,我尝试运行示例休眠应用程序

源文件: /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-12-15 09:59:43

您指定的类名不正确。而不是这样:

$JAVA_HOME/bin/java com/hib/TranslateSQL

您应该运行:

$JAVA_HOME/bin/java com.hib.TranslateSQL

您应该指定包(com.hib)内的类名,而不是文件名< /em> 在目录结构 (com/hib) 中。

编辑:如果这不起作用,则您的类路径可能设置不正确。您可以在命令行中指定它:

$JAVA_HOME/bin/java -cp . com.hib.TranslateSQL

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
痴情换悲伤 2024-12-15 09:59:43

我通过这样做让它工作:

cd  /home/chander/workspace/HibernateApp/src/com/hib/

export CLASSPATH=$CLASSPATH:/home/chander/workspace/HibernateApp/src/

$JAVA_HOME/bin/java 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文