This lesson explains how to set up a working development environment where you will be able to write, debug, monitor and run Java programs. You will also be introduced to two environment variables that play a very important part in Java development – PATH and CLASSPATH.
Installing Java Development Kit
Technically, Java is a specification and not an implementation. The spec defines the features of the language and platform and any software entity that meets those criteria is an implementation of Java. Therefore, there are many implementations of JDK. The two most notable ones are Oracle JDK (earlier Sun JDK) and IBM JDK. Although, you can go ahead with any implementation of JDK, but it is advisable to stick to Oracle JDK as much as possible. That’s what the pioneers have done and some of the smartest people are doing. Therefore, in the rest of this section, we will stick to Oracle JDK. Update: Since Oracles licensing changes re Java, you need to check whether you are downloading the JDK for personal/educational use or for commercial purposes, as this may influence which JDK will be better for you long term. Also more JDKs are available now such as the AdoptOpenJDK.
Oracle JDK can be obtained from http://www.oracle.com/technetwork/java/javase/downloads/index.html . There alongside JDK you will see links for JRE download and Server JRE downloads. Since we plant to develop Java programs, we choose JDK. You have a choice of selecting JDK + Netbeans bundle. Generally, it is a smart move to download and install from this bundle. But if you find it overwheleming to start with two things at a time, you can just go ahead with JDK download. The rest of the gig is age old – accept some license and choose your operating system. All four major platforms – Solaris, Linux, Windows and Mac OS are supported. Further, apart from Solaris and Mac OS for other operating systems there is a choice of selecting x86(32 bit) or x64 (64 bit) installation. Choose x64 if you have more than 3 GB of RAM and your operating system is 64 bit (This should be true of all modern machines).
You might be tempted to try out the package manager, such as apt on Ubuntu, provided by your operating system. Those package managers indeed work great most of the time and are great at dependency resolution. But they don’t shine when it comes to JDK installation. For one, the repository version lags behind the latest available on Oracle site. And second, you would want to avoid what most package managers install – OpenJDK. OpenJDK is an open source implementation of JDK.
Although you can use any JDK you like, but you have been warned.
Setting CLASSPATH and PATH
Once you are done with installing JDK, you need to add the programs found in $JAVA_HOME/bin to your PATH. This is more of convenience thing rather than a requirement. Adding JDK programs to your PATH ensures that
- javac and other jdk programs are accessible on your computer without specifying complete paths.
- Other JDK installation is not overriding your installation. If you already have another JDK installed on your system (possibly by someone else) your scripts might inadvertently use the old JDK programs.
Setting PATH is pretty trivial.
Setting PATH on UNIX like Systems
On UNIX like systems (Solaris, Linux, Mac OS), you just need to add the following lines to your .bashrc or .profile
|12||export JAVA_HOME= #wherever you installed JDKexport PATH=$JDK_HOME/bin:$PATH|
Of course the above snippet does not work if you are using sh, csh or tcsh or ksh. For csh shell and derivatives you need to use set instead of export.
|1234||# for csh# place these two lines in ~/.login OR ~/.cshrcset JDK_HOME = () #give the JDK installation path inside paranthesessetenv PATH $PATH\:$JDK_HOME\bin|
Setting PATH on Windows
On Windows you can set the PATH on a cmd shell or power shell by saying
|1||set path “C:\PATH_TO_JDK\bin; %path% “|
And you can also access the MyComputer -> Properties -> Advanced -> Environment Variables and change the value of path variable to permanently add JDK\bin to path.
CLASSPATH defines the path that classloader looks into to find class files. CLASSPATH is Java specific and does not affect non-Java software on your computer.
Typically, you don’t need to adjust CLASSPATH variable right after installation. You might be required to adjust CLASSPATH on a per project/application basis, rather than changing it system wide. Whenever you include an external jar (perhaps a library) in your project, you need to ensure that java (JVM which in turn invokes classloader) is able to find the jar file. This is typically done by passing the path to jar to java command as follows
|12345||java -cp <WHEREVER YOUR JAR LIES> <FILE.class> # OR java -classpath <WHEREVER YOUR JAR LIES> <FILE.class>|
If you must, though there is never a good reason for doing so, change CLASSPATH system wide you can follow the exact same steps that you did for PATH. But ponder deep before you do that.