无法运行简单的java代码
我已经为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
java 程序具有以下基本结构:
ClassName.java
尝试使用此大纲来生成代码。
编译并运行:
A java program has this basic structure:
ClassName.java
try using this outline to generate your code.
compile and run with:
我曾经在运行类文件时遇到此错误。
尝试这样做:
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.你设置你的类路径了吗?
http://download.oracle.com/javase/1.3/docs /tooldocs/win32/classpath.html
Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
您必须提供代码。
无论如何,从Java文档中, class ClassNotFoundException:
必读链接:提示:java.lang.ClassNotFoundException的原因
You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Must read link : Tip: Causes of java.lang.ClassNotFoundException
编译代码后,您很可能会编写
java filename.java
。因为这种寻求例外的情况就是在那时发生的。
使用
javac filename.java
编译程序后,使用命令启动解释器java filename
。当您输入此命令时,java 解释器会自动开始解释
filename.class
。命令:
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 interpreterjava filename
.As you enter this command java interpreter automatically starts interpreting
filename.class
.commands :