在线等,java能编译,但不能运行...环境变量是按照wiki设的

软件和网站开发以及相关技术探讨
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

在线等,java能编译,但不能运行...环境变量是按照wiki设的

#1

帖子 HelloJava » 2009-09-10 19:06

这学期要学习Java,我安装了jdk,按照wiki设置了环境变量:

代码: 全选

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
LC_CTYPE=zh_CN.UTF-8
CLASSPATH=.:/usr/lib/jvm/java-6-sun/lib
JAVA_HOME=/usr/lib/jvm/java-6-sun

代码: 全选

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

代码: 全选

javac Hello.java
可以生成Hello.class,但是

代码: 全选

java Hello.class
却显示

代码: 全选

The program 'java' can be found in the following packages:
 * gij-4.3
 * java-gcj-compat-headless
 * openjdk-6-jre-headless
 * cacao
 * gij-4.2
 * jamvm
 * kaffe
Try: sudo apt-get install <selected package>
bash: java: command not found
到底是环境变量哪里出了问题呢?先谢谢各位了

代码: 全选

/usr/lib/jvm/java-6-sun/jre/bin$ ./java
Usage: java [-options] class [args...]                   
           (to execute a class)                          
   or  java [-options] -jar jarfile [args...]            
           (to execute a jar file)                       

where options include:
    -d32          use a 32-bit data model if available

    -d64          use a 64-bit data model if available
    -server       to select the "server" VM           
                  The default VM is server.           
                                                      
    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives, 
                  and ZIP archives to search for class files.      
    -D<name>=<value>                                               
                  set a system property                            
    -verbose[:class|gc|jni]                                        
                  enable verbose output                            
    -version      print product version and exit                   
    -version:<value>                                               
                  require the specified version to run             
    -showversion  print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
头像
prisoner
帖子: 171
注册时间: 2008-07-28 14:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#2

帖子 prisoner » 2009-09-10 19:30

在PATH变量中加入 $JAVA_HOME/bin
T60-2007-DN6,T7200,2GB,120GB,14.1SXGA(1400x1050),128MB ATI Radeon X1400,DVD+RW,Intel 802.11abg wireless,DVD+RW/蓝牙/指纹/6芯
好笑的话
头像
wenstream
帖子: 186
注册时间: 2008-06-18 22:02

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#3

帖子 wenstream » 2009-09-10 19:46

sudo update-alternatives --config java
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#4

帖子 HelloJava » 2009-09-10 19:58

多谢两位的热心帮助!
prisoner 写了:在PATH变量中加入 $JAVA_HOME/bin
几经加进去了,重启后,还是bash: java: command not found

代码: 全选

There are 3 alternatives which provide `java'.

  Selection    Alternative
-----------------------------------------------
          1    /usr/bin/gij-4.2
          2    /usr/bin/gij-4.3
 +        3    /usr/lib/jvm/java-6-sun/jre/bin/java

Press enter to keep the default[*], or type selection number:
默认就是地三个,按了enter键,还是不行......
我快崩溃了,忙了好几天了,也用google搜了很长时间....
我的windows深重剧毒,已经半年没用了,我有多个系统,不想重装了,路由器坏了,9.04的pppoe上网还经常掉线
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#5

帖子 BigSnake.NET » 2009-09-10 20:30

jdk 怎么装的
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#6

帖子 HelloJava » 2009-09-10 20:39

sudo apt-get install sun-java6-jdk
我把/usr/lib/jvm/java-6-sun-1.6.0.16拷贝到~/java6了,
刚开始执行java6/bin/java Hello能够执行,现在又出现了下面的错误,真是奇怪了


代码: 全选

java6/bin/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.
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#7

帖子 BigSnake.NET » 2009-09-10 20:53

HelloJava 写了:sudo apt-get install sun-java6-jdk
我把/usr/lib/jvm/java-6-sun-1.6.0.16拷贝到~/java6了,
刚开始执行java6/bin/java Hello能够执行,现在又出现了下面的错误,真是奇怪了


代码: 全选

java6/bin/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.
不需要拷贝的

你把 jdk 重新装一次试试
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#8

帖子 HelloJava » 2009-09-10 20:58

BigSnake.NET 写了:
HelloJava 写了:sudo apt-get install sun-java6-jdk
我把/usr/lib/jvm/java-6-sun-1.6.0.16拷贝到~/java6了,
刚开始执行java6/bin/java Hello能够执行,现在又出现了下面的错误,真是奇怪了


代码: 全选

java6/bin/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.
不需要拷贝的

你把 jdk 重新装一次试试
这不是重装能解决的呀,这些天都不知道重装了多少次了.... :em06
以下是官方网站上的

代码: 全选

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. 

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

    A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename. 

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

    The Java VM requires that the class you execute with it have a main method at which to begin execution of your application. discusses the main method in detail. 
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#9

帖子 BigSnake.NET » 2009-09-10 21:03

根本没有这么复杂的, 装好后 java 就会在 PATH 里, 不知道你搞了什么搞丢了
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#10

帖子 HelloJava » 2009-09-10 21:11

BigSnake.NET 写了:根本没有这么复杂的, 装好后 java 就会在 PATH 里, 不知道你搞了什么搞丢了
我在两个系统里都试过了,没设置环境变量时javac能用,但java不能用,按网上的方法设置后也不起作用,真不知道是什么原因啊

不过仍然要感谢你们这么热心地指点我这个菜鸟
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#11

帖子 BigSnake.NET » 2009-09-10 22:45

ls -l /etc/alternatives/java
ls -l `which java` 看看
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
molisiye
帖子: 11
注册时间: 2007-06-30 14:53

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#12

帖子 molisiye » 2009-09-10 23:41

LZ。。。。。。要用java Hello
而不是java Hello.class
运行java的class文件不能带后缀名的。
头像
peachcolor
帖子: 898
注册时间: 2006-05-20 14:03

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#13

帖子 peachcolor » 2009-09-10 23:50

molisiye 写了:LZ。。。。。。要用java Hello
而不是java Hello.class
运行java的class文件不能带后缀名的。
呵呵
HelloJava
帖子: 7
注册时间: 2009-09-10 18:50

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#14

帖子 HelloJava » 2009-09-11 8:31

BigSnake.NET 写了:ls -l /etc/alternatives/java
ls -l `which java` 看看
第一个java->/usr/lib/jvm/java-gcj/jre/bin/java红色字体显示,是不是要删除java-gcj呢?
我又去官网下了jdk,安装后只能到~/jdk/bin/目录里面去运行程序,但是有点麻烦 :em06
还有个奇怪的问题,安装后我系统的字体都变了,还有java程序默认是汉语而且很多无法正常显示 :em20
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 在线等,java能编译,但不能运行...环境变量是按照wiki设的

#15

帖子 BigSnake.NET » 2009-09-11 13:19

HelloJava 写了:
BigSnake.NET 写了:ls -l /etc/alternatives/java
ls -l `which java` 看看
第一个java->/usr/lib/jvm/java-gcj/jre/bin/java红色字体显示,是不是要删除java-gcj呢?
我又去官网下了jdk,安装后只能到~/jdk/bin/目录里面去运行程序,但是有点麻烦 :em06
还有个奇怪的问题,安装后我系统的字体都变了,还有java程序默认是汉语而且很多无法正常显示 :em20
sudo update-alternatives --config java
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
回复