Question: I would like to understand the basics of how to write, compile and execute a Java program on UNIX / Linux OS. Can you explain it with a simple example?
Answer: In this article, let us review very quickly how to write a basic Hello World Java program and how to compile *.java program on Linux or Unix OS.
1. Write a Hello World Java Program
Create the helloworld.java program using a Vim editor as shown below.
$ vim helloworld.java /* Hello World Java Program */ class helloworld { public static void main(String[] args) { System.out.println("Hello World!"); } }
2. Make sure Java Compiler (javac) is installed on your system
Make sure javac is installed on your system as shown below.
$ dpkg -l | grep java ii java-common 0.30ubuntu5 Base of all Java packages ii sun-java6-bin 6-16-0ubuntu1.9.04 Sun Java(TM) Runtime Environment (JRE) 6 (ar ii sun-java6-jdk 6-16-0ubuntu1.9.04 Sun Java(TM) Development Kit (JDK) 6 ii sun-java6-jre 6-16-0ubuntu1.9.04 Sun Java(TM) Runtime Environment (JRE) 6 (ar ii sun-java6-plugin 6-16-0ubuntu1.9.04 The Java(TM) Plug-in, Java SE 6 $ whereis javac javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz $ which javac /usr/bin/javac
3. Compile the helloworld.java Program
Compile the helloworld.java using javac command as shown below. This will create the helloworld.class file.
$ javac helloworld.java $ ls -ltr total 1760 -rw-r--r-- 1 ramesh ramesh 149 2010-01-23 09:51 helloworld.java -rw-r--r-- 1 ramesh ramesh 426 2010-01-23 09:52 helloworld.class
4. Execute the Java Class Program (helloworld.class)
Execute helloworld.class as shown below.
$ java helloworld Hello World!
Comments on this entry are closed.
i m using rhel5 ..how can i install java compiler?
.
sudo yum install javacc
thank you sir.
you have told about how to create and execute java program…
Thanks!!! Fabulous! I’m now figuring out how to do the different scripts in the shell. Wild.
thank u sir,
you have told about how to create and execute java program
Hello sir.this is santhosh just I’ve completed my 12th class.I am very eager to know the java programming.can you help me.if you help me I’ll become a very good java programmer.
@ Santhosh
Go to book store or online shop and buy a copy of “Head first Java” book,
You will really love to study java in Head first java book.
Happy learning
Regards,
K
Thank u sir.I kind of u
Thank you sir. Java programs seems to be very interesting , i can now solve some of the related programs like java hello world
Run java -cp . helloworld