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!
If you enjoyed this article, you might also like..
|
|
|
|











My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 3 comments… read them below or add one }
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…