Saturday, November 22, 2008

java-entrance

>>>>>>>install jre & jdk in unbuntu

There are two method to install jdk in ubuntu:

To setup the jdk env, you must read this through firstly.


1. manual installation

go to http://java.sun.com/ to download jdk. There is a detail to be pointed out, that is you should download the .bin, NOT the .rpm.bin version.
(The rpm.bin version work in redhat relative version.)

Then, you can move it to /opt with the sudo and unpack it there.

cd ~/Desktop
sudo mv java_filename.bin /opt
cd /opt
sudo chmod 755 java_filename.bin
sudo ./java_filename.bin

That would unpack it in /opt. Now, you just have to configure it so your system would use the new version.
----------------the above steps are working...

Code:
sudo update-alternatives --config java
And make sure you pick the new version from the list. Now, you can check to see if your system is using the new version with

Code:
java -version


2. using apt-get to install it

Installing Java Runtime and JDK has been made easy since Ubuntu Feisty Fawn since multiverse and universe package has been made available by default. This short how to, covers an easy way to install Sun Java environment in your Ubuntu operating system.

Installing Java Runtime

$ sudo apt-get install sun-java6-jre

Installing Java Software Development Kit (JDK)

$ sudo apt-get install sun-java6-jdk

Alternatively you can choose to install previous Sun Java version on your machine by replacing 'sun-java6' with 'sun-java5' for Java 1.5, or by installing 'j2sdk1.4' package for Java 1.4.

That's all, you should be able to run Java applets using your favorite browser or run Java application by now.

However, if you install the jdk by this way, maybe some packages such as src of jdk lib will not be install or the install directory hardly to found.

To install all the things, you can use:
$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk


Setup the default Java version

Ubuntu Linux comes with update-java-alternatives utility to updates all alternatives belonging to one runtime or development kit for the Java language. To select, Sun's JVM as provided in Ubuntu 7.10, enter:
$ sudo update-java-alternatives -s java-6-sun

>>>>>>>>
You also need to edit a file called /etc/jvm. This file defines the default system JVM search order. Each JVM should list their JAVA_HOME compatible directory in this file. The default system JVM is the first one available from top to bottom. Open /etc/jvm
$ sudo vi /etc/jvm
Make sure /usr/lib/jvm/java-6-sun is added to the top of JVM list
/usr/lib/jvm/java-6-sun
At the end your file should read as follows:
/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-gcj
/usr/lib/jvm/ia32-java-1.5.0-sun
/usr/lib/jvm/java-1.5.0-sun
/usr

Save and close the file.
-------set jvm path here.

Setup the environment variable

You also need to setup JAVA_HOME and PATH variable. Open your $HOME/.bash_profile or /etc/profile (system wide) configuration. Open your .bash_profile file:
$ vi $HOME/.bash_profile
Append following line:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file.

-----set JAVA_HOME and PATH
but it seems it do not work after set the profile. or set it wrong.
and i can not find $HOME/.bash_profile. why ??

If you can not find the .bash_profile, you can and these things to $HOME/.bashrc .

Or, you can create a new .bash_profile file in the $HOME directory!!!


Test your new JDK

Type the following command to display version:
$ java -version
Output:

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)

Try HelloWorld.java - first java program

$ vi HelloWorld.java
Append code:

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

Save and close the file. Compile HelloWorld.java and execute program by typing following two instructions:
$ javac HelloWorld.java
$ java HelloWorld

Output:

Hello, World!

---------------------more info

Currently Ubuntu has the following Java packages

sun-java6-bin - Contains the binaries

sun-java6-demo - Contains demos and examples

sun-java6-doc - Contains the documentation

sun-java6-fonts - Contains the Lucida TrueType fonts from the JRE

sun-java6-jdk - Contains the metapackage for the JDK

sun-java6-jre - Contains the metapackage for the JRE

sun-java6-plugin - Contains the plug-in for Mozilla-based browsers

sun-java6-source - Contains source files for the JDK

Installing the Java Runtime Environment

First you need to check multiverse repository enabled or not after that open a terminal window. Since you are going to be installing the JRE and the web browser plug-in, you’ll be using the following command from a terminal

sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts

Once it downloads the packages and begins the installation, you’ll get a screen that contains the Sun Operating System Distributor License for Java and hit Enter to continue. You’ll see a dialog that asks you if you agree with the DLJ license terms. Select Yes, and hit Enter; the JRE will finish installing.

Testing Java Runtime Environment

You’ll want to confirm that your system is configured properly for Sun’s JRE. This is a two-step process.

First, check that the JRE is properly installed by running the following command from a terminal.

java -version

You should get similar output

java version “1.6.0″
Java(TM) SE Runtime Environment (build 1.6.0-b105)

Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Testing Java Plugin for Firefox

open Firefox and typing about:plugins in the address bar and check for java plugin

>>>>>>>>>>>>>
1.......

2.设置环境变量。
#vi /etc/profile
在最后面加入
#set java environment
JAVA_HOME=/usr/java/jdk-1_5_0_02
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
保存退出。

要使JDK在所有的用户中使用,可以这样:
vi /etc/profile.d/java.sh
在新的java.sh中输入以下内容:
#set java environment
JAVA_HOME=/usr/java/jdk-1_5_0_02
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
保存退出,然后给java.sh分配权限:chmod 755 /etc/profile.d/java.sh

3.在终端使用echo命令检查环境变量设置情况。
#echo $JAVA_HOME
#echo $CLASSPATH
#echo $PATH

4.检查JDK是否安装成功。
#java -version
如果看到JVM版本及相关信息,即安装成功!

>>>>>>>>>>>>sample(in ubuntu)

1. you can use eclipse to edit/compile/run the java. if you can run the eclipse( once you setting the jdk in the right way, you can run eclipse), then you can run the java sample easily.

2.using javac to compile the java source code and java to run the exe.

-------------the env running the sample

simon@simon-laptop:~/workspace$
simon@simon-laptop:~/workspace$ ls
firstJavaSample helloworld log testjava1
simon@simon-laptop:~/workspace$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/java/jdk1.6.0_10/bin
simon@simon-laptop:~/workspace$ echo $JAVA_HOME
/usr/java/jdk1.6.0_10
simon@simon-laptop:~/workspace$ echo $CLASSPATH

simon@simon-laptop:~/workspace$

---------the java source file name and directory

simon@simon-laptop:~/workspace/firstJavaSample$ ls
FirstJavaSample.class FirstJavaSample.java

----------the code of the source file
package firstJavaSample;


public class FirstJavaSample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello world");

}
}
-----------compile and run the java exe
simon@simon-laptop:~/workspace/firstJavaSample$ javac FirstJavaSample.java
simon@simon-laptop:~/workspace/firstJavaSample$ cd ..
simon@simon-laptop:~/workspace$ java firstJavaSample.FirstJavaSample
hello world
simon@simon-laptop:~/workspace$

-------------some things you should care about

1. the name of the folder that contains the java source code should be the name of package of the the java soure code (firstJavaSample)

2.the name of the public class should be the name of the soure file (FirstJavaClass)

3. case senstive (firstJavaSample ! = FirstJavaSample)

4. you should go back to the folder that contains the package folder to run the java exe.

simon@simon-laptop:~/workspace$ java firstJavaSample.FirstJavaSample

workspace contains the firstJavaSample folder


No comments: