`
美丽的小岛
  • 浏览: 299158 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

java&linux

阅读更多

    为了做一个大象(hadoop)的实验,构造一个实验环境( windows  xp + VMWare7 + linux + jdk + eclipse + tomcat  ),零风险的情况下去做实验。选择了VMWare这个虚拟的东西,用的版本为7.0。在虚拟机上,刚开始的时候想用Ubuntu来构建的, 弄了两天,那个VMWare Tools总有问题,那些权限问题,当成功装上了这个Tools时,出现网络连接不上,加上设置文件共享也没有成功。后来,放弃之。用了Red Hat9,这个跟我亲和多了,很顺利就完成了,还配置了一个文件的共享(E:/redhatfile),网络还好用,呵呵。下面是一个在linux上安装JDK.有三个步骤:

 

1.         寻找JDK

下载网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

找一个合linux条件的下载。下载到E:/redhatfile的目录下(方便与linux共享)。下载文件的名为:jdk-6u31-linux-i586.bin

 

2.         安装JDK

    开启Red Hat,输入root与密码进入。usr目录中创建一个文件夹java.jdk-6u31-linux-i586.bin复制到/usr/java的目录下。具体做法,可以用命令,也可以在界面上像Windows那样去操作。我用了命令符:

       cd  /mnt/hgfs/redhatfile

      cp  jdk-6u31-linux-i586.bin /usr/java

      这样就把jdk-6u31-linux-i586.bin放到了/usr/java文件夹下。

   安装也简单:

     cd  /usr/java

     ./ jdk-6u31-linux-i586.bin

   就行了,当这个命令执行完了,在java文件夹就会多一个jdk1.6.0_31文件。

 

3.         配置JDK

这个就是修改一个文件profile,这个文件在/etc目标下。

vi /etc/profile

profile文件后面加入:

export JAVA_HOME=/usr/java/ jdk1.6.0_31

export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin(不要漏了“$PATH:)

export CLASSPATH=.:$JAVA_HOME/lib/tools.jar(不要漏了“.:”)

退出到命令行,进行对frofile进运行一下:

source /etc/profile

检查一下是否都配置上了,用echo:

echo $PATH

或用javac一下,会内容的,不成功自然会报错。

总结,漏了“$PATH:”试了一次,使系统重启后什么进不了。

漏了“.:”,这个使java文件找不到class在哪里。”.”代表当前目录。

 

4.         测试JDK

写一个HelloWorld,因为依赖了ECLIPSE,所以测试时会遇到以下的问题:

文件:HellWorld.java

public class HelloWorld{

         public static void main(String[] arg){

                   System.out.println(“hello world!”);

}

}

命令如下(错误的):

javac  HelloWorld.java

java  HelloWorld.class

结果:

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

Caused by: java.lang.ClassNotFoundException: HelloWorld.class

        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Could not find the main class: HelloWorld.class.  Program will exit.

插入一个老外的内容吧,是看这个东西才醒过来,后来解决了。

Most people always have the following problem when run their java programs under linux box:
Exception in thread "main" java.lang.NoClassDefFoundError: *.java
Even though these programs  really have no problem at all.
Here are some solutions for you:
1) Check your syntax that you are trying to run the java class and make sure it should be like this (assume the class file is foo.class):
java  foo      instead of    java  foo.class

2) Make sure your classpath contain the special path ".", actually this is most likely where the problem come from. 
You can try to run your program as follows.

java -cp .  foo   or
java  -classpath  .  foo

If it works, then you are suppose to add "." to you classpath.
For example:
export CLASSPATH=.:$JAVA_HOME/lib:JAVA_HOME/jre/lib

3) Check it out if you compiled the java file without specifying the package that import in your file.

What if all these works are done, but your still got the same problem?
Well, in this case, you've got to have a look at your souce code......

Good luck!
我犯了第一种情况,因为加上了.class 有点大意,从另外一个侧面来说明很少这样去运行一个java文件,有点依赖开发工具了(eclipse),修改过来可以了。

 

5.         延伸一下

对于一个带有包的java文件,怎样去运行?

    package test ;

    public class HelloWorld{

         public static void main(String[] arg){

                   System.out.println(“hello world!”);

}

}

   正确的编译与执行:

     javac –d classes HelloWorld.java

    cd  classes

    java  test. HelloWorld;

  其它的方法都会出现上面的错误情况。

  关于javac总结,这个网址总结的很好:   http://hubingforever.blog.163.com/blog/static/171040579201151722719321/

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics