≡ Menu

Java Hello World Example: How To Write and Execute Java Program on Unix OS

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!
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • jameslee February 17, 2010, 11:45 pm

    i m using rhel5 ..how can i install java compiler?
    .

  • jo3r October 22, 2011, 10:49 pm

    sudo yum install javacc

  • sirsath manoj January 8, 2012, 12:58 am

    thank you sir.
    you have told about how to create and execute java program…

  • Elaine Morrison March 16, 2013, 1:05 pm

    Thanks!!! Fabulous! I’m now figuring out how to do the different scripts in the shell. Wild.

  • andu March 29, 2013, 8:42 am

    thank u sir,
    you have told about how to create and execute java program

  • santhosh June 8, 2013, 10:02 am

    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.

  • Kainiteh June 26, 2013, 6:27 am

    @ 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

  • santhosh June 27, 2013, 7:18 am

    Thank u sir.I kind of u

  • Judith Molepo February 12, 2015, 10:50 am

    Thank you sir. Java programs seems to be very interesting , i can now solve some of the related programs like java hello world

  • Saurabh April 25, 2017, 1:55 am

    Run java -cp . helloworld