1.Getting and Installing the JDK
We start by downloading the JDK. For most people, the standard edition of the Java Development Kit (J2SE) will work fine. However, if you need support for EJB (Enterprise Java Beans), you should get the J2EE (Enterprise Edition) instead. In either case, point your browser at java.sun.com/download.html, where you can get both J2SE and J2EE. At the time of this writing, the latest version of the J2SE SDK is 1.4.0 (about 26 Mb). My recommendation is to get the BIN-version of the file, although the RPM-package works if you're using an RPM-based distribution. BIN gives you more control over how and where you want to have your Java-environment installed. And that can be nice when you're dealing with large packages like this one.
The file should be on your local machine, so it's time to start the installation (make sure the file has permissions 755):
root@localhost:~# ./j2sdk-1_4_0_01-linux-i586.bin
As you can see, I choose to execute the file as root. You could do this as a regular user as well, but you would not be able to install it in a system-wide directory.
After you've read the license agreement, you will be asked
Do you agree to the above license terms? [yes or no]
Type yes and press Enter. The file-extraction process will begin now, placing all the files in a sub-directory below your current working directory (cwd). The most common location for Java installations is /usr/local, so we'll go with that:
root@localhost:~# mv j2sdk-1_4_0_01-linux-i586.bin /usr/local
Personally, I think it's nice to always have JDK installed in /usr/local/jdk but still be able to easily see what version is used. To accomplish this, you can simply do:
root@localhost:~# ln -s /usr/local/ /usr/local/jdk
This creates a symbolic link, /usr/local/jdk, pointing to the /usr/local/jdk1.4.0_01 -directory. If you ever upgrade your JDK, all you need to do is update the symbolic link, and the Java compiler (for example) will still reside in /usr/local/jdk/bin, saving you from the work of changing your PATH variable and/or updating scripts that use files in /usr/local/jdk. Executing a ls -als in /usr/local should output something like this:
root@localhost:/usr/local# ls -als 4 drwxrwsr-x 17 root staff 4096 Aug 13 18:31 ./ 4 drwxr-xr-x 14 root root 4096 Aug 1 14:29 ../ 4 drwxr-xr-x 2 root staff 4096 Jun 13 21:15 bin/ 4 drwxr-xr-x 8 root staff 4096 Aug 13 18:31 j2sdk1.4.0_01/ 0 lrwxrwxrwx 1 root staff 24 Aug 13 18:31 jdk ->/usr/local/j2sdk1.4.0_01/ 4 drwxrwsr-x 6 root staff 4096 Aug 1 14:28 lib/ 4 drwxrwsr-x 6 root staff 4096 Oct 22 2001 man/ 4 drwxrwsr-x 2 root staff 4096 Jun 13 21:17 sbin/ 4 drwxrwsr-x 9 root staff 4096 Mar 5 13:31 share/ 4 drwxrwsr-x 2 root staff 4096 Apr 15 2001 src/
Speaking of the PATH variable, that is also something we need to take care of--if you want to be able to access the executables in /usr/local/jdk/bin, that is. As a Java developer, you will need to access these files quite often, so it's generally a good idea to set it up.
Fortunately, editing the PATH variable is a simple task. All you have to do is to add the line below to your /etc/profile (or whatever file holds your global shell variables).
export PATH=$PATH:/usr/local/jdk/bin
This means set the variable PATH to the previous value of PATH, and add /usr/local/bin/jdk to it. Then, reload your /etc/profile by issuing:
root@localhost:~# source /etc/profile
You can then test to see if your shell can find the Java compiler simply by typing javac. If everything is okay, you should see something like:
root@localhost:~# javac Usage: javac where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -O Optimize; may hinder debugging or enlarge class file -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath Specify where to find user class files -sourcepath Specify where to find input source files -bootclasspath Override location of bootstrap class files -extdirs Override location of installed extensions -d Specify where to place generated class files -encoding Specify character encoding used by source files -source Provide source compatibility with specified release -target Generate class files for specific VM version -help Print a synopsis of standard options
If you get a command not found message instead, make sure your PATH variable is set up correctly, and then try again.
In addition to PATH, another variable needs to be defined. JAVA_HOME defines where on your filesystem the JDK base directory can be found. In our case, this is /usr/local/jdk. Other applications that use Java (such as Resin, which we will look at a little later) use this variable to find the JDK. Add this line to your /ets/profile:
export JAVA_HOME=/usr/local/jdk
And reload /etc/profile:
root@localhost:~# source /etc/profile
Time for testing.
2.Testing Your JDK
You should now have a working Java development environment. To test this, we will try to compile and run a simple Java class. Enter the following code into a file named Test.java:
import java.lang.reflect.Array;
class Test { public static void main( String argv[] ) { System.out.println( "Arguments:" );
int i = 0;
while( i < Array.getLength(argv) ) { System.out.println( argv[i] ); i++; }
}
}
Then, try to compile it with the following command:
root@localhost:~# javac Test.java
And finally, execute your class by issuing this command:
root@localhost:~# java Test argument1 argument2 argument3 Arguments: argument1 argument2 argument3
As you see, this simple program prints the command-line arguments given and then exits. If this worked, it's safe to say your installation was successful.
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=103767