螺纹“主”螺纹中的异常java.lang.NoClassDefFoundError:HelloWorld [英] Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

查看:198
本文介绍了螺纹“主”螺纹中的异常java.lang.NoClassDefFoundError:HelloWorld的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个工作约一个小时,在stackoverflow拇指通过Q& As,但我没有找到一个建议的解决方案,我的问题。对不起,如果这是一个重复,但我找不到任何重复的问题,答案解决了我的具体问题。



我试图写和编译一个java程序从终端第一次(直到这一点,我一直使用Eclipse的java和VIM的一切,但我觉得它的时间完全切换到VIM)。这是我当前的HelloWorld代码:

  package main; 

public class HelloWorld {
public static void main(String args []){
System.out.println(Hello World!
}
}

我使用以下命令编译和运行classpath,以确保不是问题):

  javac -cp./HelloWorld.java 
java -cp./HelloWorld

这会给我以下错误信息:



线程main中的异常java.lang.NoClassDefFoundError:HelloWorld(错误的名称:main / HelloWorld)
在java.lang.ClassLoader。 defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
在java。 net.URLClassLoader.defineClass(URLClassLoader.java:449)
在java.net.URLClassLoader.access $ 100(URLClassLoader.java:71)
在java.net.URLClassLoader $ 1.run(URLClassLoader.java: 361)
在java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)
在java.security.AccessController.doPrivileged(本地方法)
在java.net.URLClassLoader.findClass (URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

我知道它正在查看文件HelloWorld.class并试图访问类HelloWorld,因为如果我将运行命令更改为:

  java -cp./Foo 

我得到一个完全不同的错误消息:

 错误:找不到或加载主类Foo 

我已经尝试了几十个页面的故障排除和简短,包括以下:



线程中的异常main java.lang.NoSuchMethodError:main



http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html



java -version 产生:

  java版本1.7.0_07
Java(TM)SE运行时环境(构建1.7.0_07-b10)
Java HotSpot(TM)客户机VM(构建23.3-b01,混合模式)
pre>

我的操作系统是LinuxMint, uname -a 产生:

  Linux will-Latitude-D620 2.6.38-8-generic#42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU / Linux 


解决方案


package main;


这意味着你的类驻留在 main 包中,其规范名称为 main.HelloWorld



Java要求软件包名称也应镜像到目录结构中。这意味着:


  1. 您的 HelloWorld.java 文件应位于 main

  2. 您应该执行 javac java从 main ,而不是本身的目录
  3. $ b $ b
  4. 类路径应包含 main 目录所在的目录,而不是 main 本身

  5. java 需要执行类的规范名称,因此 main.HelloWorld

所以,总结一下:



你应该有 myproject / main / HelloWorld.java



myproject ,运行 javac main / HelloWorld.java



myproject ,运行 java -cp ./ main.HelloWorld


I've been working on this for about an hour and thumbing through Q&As on stackoverflow but I haven't found a proposed solution to my problem. I'm sorry if this is a duplicate, but I couldn't find any duplicate question with an answer that solved my specific problem.

I am trying to write and compile a java program from terminal for the first time (up until this point I have been using Eclipse for java and VIM for everything else, but I feel its time to switch entirely to VIM). Here is my current HelloWorld code:

package main;

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

I compile and run using the following commands (specifying the classpath to ensure that isn't the problem):

javac -cp "./" HelloWorld.java
java -cp "./" HelloWorld

This gives me the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: main/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

I know it is seeing the file HelloWorld.class and trying to access the class HelloWorld because if I change the run command to:

java -cp "./" Foo

I get an entirely different error message:

Error: Could not find or load main class Foo

I have tried several dozen pages worth of troubleshooting and come up short, including the following:

Exception in thread "main" java.lang.NoSuchMethodError: main

http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html

java -version yields:

java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode)

My operating system is LinuxMint and uname -a yields:

Linux will-Latitude-D620 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux

解决方案

package main;

This means that your class resides in the main package, and its canonical name is main.HelloWorld.

Java requires that package names should also be mirrored in the directory structure. This means that:

  1. Your HelloWorld.java file should be in a directory named main
  2. You should execute javac and java from the directory containing main, not from main itself
  3. The classpath should contain the directory where the main directory is, not main itself
  4. java expects the canonical name of the class to execute, so main.HelloWorld

So, to recap:

You should have something like myproject/main/HelloWorld.java

From myproject, run javac main/HelloWorld.java

From myproject, run java -cp ./ main.HelloWorld

这篇关于螺纹“主”螺纹中的异常java.lang.NoClassDefFoundError:HelloWorld的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆