无法运行简单的java代码

发布于 2024-11-17 23:46:16 字数 757 浏览 1 评论 0原文

我已经为我的 64 位 Windows 7 下载了一个 java 开发人员工具包,在记事本中写下了我的代码,尽管代码是从命令提示符编译并创建 .class 文件,但它拒绝运行,显示错误代码:

java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first   
  at java.net.URLClassLoader$1.run(Unknown Source)   
  at java.security.AccessController.doPrivileged(Native Method)   
  at java.net.URLClassLoader.findClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source)   
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source) 
Could not find the main class: first.  Program will exit. Exception in thread "main"

我已经做了不止一次确保文件名和类名完全相同(为了确定,我将它们保留为小写“a”)。但仍然无济于事,请你建议一些解决方案好吗?我是java新手,我基本上是一个C/C++程序员。

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:

java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first   
  at java.net.URLClassLoader$1.run(Unknown Source)   
  at java.security.AccessController.doPrivileged(Native Method)   
  at java.net.URLClassLoader.findClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source)   
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source) 
Could not find the main class: first.  Program will exit. Exception in thread "main"

I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

匿名。 2024-11-24 23:46:16

java 程序具有以下基本结构:

ClassName.java

public class ClassName
{
    public static void main(String[] args)
    {

    }
}

尝试使用此大纲来生成代码。

编译并运行:

javac ClassName.java
java ClassName

A java program has this basic structure:

ClassName.java

public class ClassName
{
    public static void main(String[] args)
    {

    }
}

try using this outline to generate your code.

compile and run with:

javac ClassName.java
java ClassName
我乃一代侩神 2024-11-24 23:46:16

我曾经在运行类文件时遇到此错误。

尝试这样做: java NameOfClass

运行时不需要 .java 扩展名,但编译时需要它。这一直是我曾经遇到的问题。

I used to get this error when I ran a Class file.

Try doing: java NameOfClass

You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.

蘑菇王子 2024-11-24 23:46:16

你设置你的类路径了吗?

http://download.oracle.com/javase/1.3/docs /tooldocs/win32/classpath.html

java -classpath <path> <classname>

Did you set your classpath?

http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html

java -classpath <path> <classname>
所谓喜欢 2024-11-24 23:46:16

您必须提供代码。

无论如何,从Java文档中, class ClassNotFoundException:

当应用程序尝试执行以下操作时抛出
通过字符串加载类
名称使用:

  • Class 类中的 forName 方法。
  • ClassLoader 类中的 findSystemClass 方法。
  • ClassLoader 类中的 loadClass 方法。

但没有指定名称的类的定义
可以找到。

必读链接:提示:java.lang.ClassNotFoundException的原因

You must provide with the code.

Anyway, from the Java Docs, class ClassNotFoundException:

Thrown when an application tries to
load in a class through its string
name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name
could be found.

Must read link : Tip: Causes of java.lang.ClassNotFoundException

楠木可依 2024-11-24 23:46:16

编译代码后,您很可能会编写java filename.java
因为这种寻求例外的情况就是在那时发生的。

使用javac filename.java编译程序后,使用命令启动解释器java filename

当您输入此命令时,java 解释器会自动开始解释 filename.class

命令:

javac filename.java   // to start compiling
java filename         // to start interpreting

It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.

After compiling your program using javac filename.java use the command to start your interpreter java filename.

As you enter this command java interpreter automatically starts interpreting filename.class.

commands :

javac filename.java   // to start compiling
java filename         // to start interpreting
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文