Java is one of the most widely used, flexible,
multiplatform , object-oriented language. Hence knowing what is java
and how it works is more important for any software developer, even if
he is not working in a java domain. This is because, nowadays many
projects are developed with J2EE technology and knowing how things work
in java and the technology oriented with it is always an added
advantage. And this article will help learners from scratch, to know
how java programming can be done in a gnu/linux distro.
To start with, in this article we explain how java can be installed in
a gnu system. This is a common article and here we are not considering
any package management tools like rpm, apt etc.
Java is packaged and the developement packages are bundled into a kit
named as Java Development Kit. We need to download that kit to proceed
further.
Just drop into this website and download your copy of the latest j2sdk for gnu/linux platforms.
?
http://java.sun.com/j2se/1.4.2/download.html
In the above link select the “The J2SE Software Development Kit (SDK)”
and this J2SDK will have the JRE (runtime environment for java) bundled
with it. After clicking that link, it leads to another page with a list
of jdks for various platforms. Across “Linux” platform there are 2
files given, one is an RPM and another file is an executable (bin).
Download the executable file. This jdk can be installed in any
gnu/linux system. Ofcourse you need to accept a licence from SUN
Microsystems before you start your download.
We are trying things in a type of GNU system named “Vector Linux”, with the 2.6.11 kernel.
After download is over, just check the “execute” permission for the
downloaded file, which in my case is “j2sdk-1_4_2_08-linux-i586.bin”.
Checking the execute permissions:
??
?root:# ls -l
?drwxr-xr-x 2 root root 4096 2005-05-25 15:10 Trash/ ?-rw-r–r– 1 root root 36418360 2005-07-04 17:54 j2sdk-1_4_2_08-linux-i586.bin ?-rw-r–r– 1 root root 1161 2005-07-04 17:45 java_gnu |
?
It is evident that there is no execute permission for the
“j2sdk…..bin” file. We need to modify it. Just a “chmod” would solve
this problem
?root:#chmod 777 j2sdk-1_4_2_08-linux-i586.bin |
?
So, now everything is set for installing the java Development Kit in a GNU system.
We execute the jdk..bin file
?? root:#./j2sdk-1_4_2_08-linux-i586.bin |
This results in producing a Licence agreement screen. Read the entire
licence agreement by pressing spacebar. Finally a question is asked,
?Do you agree to the above license terms? [yes or no] |
Type “yes” as answer and then proceed (do this only if you agree with those licencing terms)
Thats it, the package will be unpacked and installed. Finally you get a “Done” message.
To Check whether java files can be compiled and executed we just type “javac” in the shell. we get this..
?root:# javac
?-bash: javac: command not found? |
Interesting, jdk is installed without any error, but commandline is not
recognising the java compiler. yes, the reason behind this is the
location where the jdk is installed is not known to the shell to search
the command for execution. In other words we can say, PATH is not set
right. There is an environment variable PATH. To view the current value
of ‘PATH’
?? root:#echo $PATH ?/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/opt/bin :/usr/local/games:/opt/java/bin:/opt/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin :/usr/local/sbin:/usr/sbin:/sbin |
Java Development Kit is installed in the home directory of the user, in
my case it is the “/root” directory. So check out the path similar to
this “/j2sdk1.4.2_08/bin” in the path that is displayed. It is not
found, hence we need to append the PATH variable with the path to the
java compiler. We can do this by typing the following in the shell
?root:# PATH=$PATH”:/root/j2sdk1.4.2_08/bin” |
Check out the content of the PATH now
root:# echo $PATH ?/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/opt/bin :/usr/local/games:/opt/java/bin:/opt/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:/usr/local/sbin :/usr/sbin:/sbin:/root/j2sdk1.4.2_08/bin? |
?
You can find that j2sdk path is appended to the end of the path content.
Now check whether your shell identifies “javac”. Type “javac” in the shell prompt.
You should get this…
?
root:# 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 ?-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 this, things are working in our way.
Now lets write a simple java program using any of your favourite editors.
Code:
?
?class eg_1 ? { ??? public static void main(String a[]) ???? { ?????? System.out.println(“Welcome to GNU World”); ???? } ? } |
Save the file as “eg_1.java”, the file name should be same as the class
name and hence we save the file that way. Now in the shell go to the
location where you saved the java file.
Compiling Java Code:
root:# javac eg_1.java root:# |
If there are no errors in our code, i mean compilation errors, then we
get the shell prompt. Now the file named “eg_1.class” is created. check
it out using the ls command in the directory where the java file is
resident.
Executing Java Code:
?root:# java eg_1
?Welcome to GNU world |
Hope this article is clear, for any feedbacks please write to the webmaster[at]gnulinuxclub.org