线程“main"中的异常java.lang.NoClassDefFoundError:HelloWorld [英] Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

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

问题描述

我已经为此工作了大约一个小时,并在 stackoverflow 上浏览了 Q&As,但我还没有找到解决我的问题的建议方案.如果这是重复的,我很抱歉,但我找不到任何重复问题的答案可以解决我的特定问题.

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.

我第一次尝试从终端编写和编译一个 java 程序(到目前为止,我一直在使用 Eclipse 编写 java 和使用 VIM 编写其他所有程序,但我觉得是时候完全切换到 VIM 了).这是我当前的 HelloWorld 代码:

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)

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

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:

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

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

java -version 产生:

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)

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

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

推荐答案

包主;

这意味着您的类位于 main 包中,其规范名称是 main.HelloWorld.

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

Java 要求包名也应该反映在目录结构中.这意味着:

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

  1. 您的HelloWorld.java 文件应该位于名为main
  2. 的目录中
  3. 您应该从包含 main 的目录中执行 javacjava,而不是从 main 本身
  4. classpath 应该包含 main 目录所在的目录,而不是 main 本身
  5. java 需要类的规范名称来执行,所以 main.HelloWorld
  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

所以,回顾一下:

你应该有类似 myproject/main/HelloWorld.java

myproject,运行 javac main/HelloWorld.java

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

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

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