java编译成功运行错误

软件和网站开发以及相关技术探讨
回复
07jhliang3
帖子: 9
注册时间: 2009-03-25 20:26

java编译成功运行错误

#1

帖子 07jhliang3 » 2009-03-29 9:43

新手疑问:
操作系统版本:Ubuntu 8.10
JAVA版本:JAVA 6

代码: 全选

sudo apt-get install sun-java6-jre sun-java6-jdk
安装成功

代码: 全选

$sudo gedit /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
LANGUAGE="zh_CN:zh:en_US:en"
LANG="zh_CN.UTF-8"
CLASSPATH=.:/usr/lib/jvm/java-6-sun/lib
JAVA_HOME=/usr/lib/jvm/java-6-sun

代码: 全选

$sudo gedit /etc/jvm 
空白,只加了一行:/usr/lib/jvm/java-6-sun
源码文件:Hello.java
public class Hello{

public static void main(String args[]){

System.out.println("Hello World in Ubuntu!!");
}

}
编译与运行:
jh@ubuntu:~$

代码: 全选

 javac Hello.java
jh@ubuntu:~$

代码: 全选

java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
Caused by: java.lang.ClassNotFoundException: Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Hello. Program will exit.

ps:昨天刚刚装好的时候运行ok的,但是重启后就有了上面的错误了
07jhliang3
帖子: 9
注册时间: 2009-03-25 20:26

Re: java编译成功运行错误

#2

帖子 07jhliang3 » 2009-03-29 9:53

哪位朋友知道的麻烦告诉一下原因..........在线等
Gabyong
帖子: 1
注册时间: 2009-05-11 11:10

Re: java编译成功运行错误

#3

帖子 Gabyong » 2009-05-11 11:13

我遇到了同样的问题,在java的官网找到了答案,附上:

Error Messages on UNIX Systems

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

One of the places java tries to find your bytecode file is your current directory. So, for example, if your bytecode file is in /home/jdoe/java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Return:

cd /home/jdoe/java

If you enter pwd at the prompt, you should see /home/jdoe/java. If you enter ls at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

If you still have problems, you might have to change your CLASSPATH environment variable. To see if this is necessary, try clobbering the classpath with the following command.

unset CLASSPATH

Now enter java HelloWorldApp again. If the program works now, you'll have to change your CLASSPATH variable in the same manner as the PATH variable above.

详情见:
http://java.sun.com/docs/books/tutorial ... index.html
回复