How to Compile and Run Java Program

By | January 25, 2016

We will learn How to Compile and Run Java Program – The Java Basic Syntax – “Hello World” My First Program in Java

In our previous article, We will learn The Java Basic Syntax. In this article, we will make our First program in Java (Hello World), We will learn How to Compile and Run Java Program in your machine. The most basic syntax, keywords, special words and the working environment of Java.

Enough background! Let’s finish off this day by creating a real Java program. In Java you either can make a stand-alone Java application or an applet that you can view in either in the applet viewer (part of the JDK) or in a Java-capable browser.

Although our first program is extremely simple, it will give you an idea of what a Java program looks like and how to compile and run it.

Java Applets: Applets, are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the reader’s machine. Applets depend on a Java-capable browser in order to run.

Java Applications: Java applications are more general programs written in the Java language. Java applications don’t require a browser to run, and in fact, Java can be used to create most other kinds of applications that you would normally use a more conventional programming language to create. HotJava itself is a Java application. You may also like to read Getting Started with JAVA Language – An Overview to JAVA.

how to compile and run java program - My first Java Program [howpk.com]

how to compile and run java program – My first Java Program [howpk.com]

“Hello World” My First Java Program:




Let us now briefly look into what do class, object, methods and instance variables mean.

  • Object – Objects have states and behaviors. Example: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
  • Class – A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
  • Methods – A method is basically a behavior. A class can contain many methods. It is in methods where the logic are written, data is manipulated and all the actions are executed.
  • Instance Variables – Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.

First Java Program – Print “Hello World” on Screen:

public class MyFirstJavaProgram{
/* This is my first java program. 
     * This will print ‘Hello World’ as the output
     */
public static void main(String[]args){
System.out.println(“Hello World”);// prints Hello World
}
}

How to Compile and Run Java Program:

  1. Open notepad and add the code as above.
  2. Save the file as “MyFirstJavaProgram.java” (or any other name of your desire).
  3. Open a command prompt window and go to the directory where you saved the class. Assume it’s C:\.
  4. Type “javac MyFirstJavaProgram.java” and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line(Assumption : The Java path variable is set).
  5. Now, type “java MyFirstJavaProgram” to run your program.
  6. You will be able to see “Hello World” printed on the window.

Basic Syntax:

About Java programs, it is very important to keep in mind the following points.

  • Case Sensitivity – Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
  • Class Names – For all class names, the first letter should be in Upper Case.   If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case.  Example class MyFirstJavaClass.
  • Method Names – All method names should start with a Lower Case letter.   If several words are used to form the name of the method, then each inner word’s first letter should be in Upper Case.  Example public void myMethodName().
  • Program File Name – Name of the program file should exactly match the class name.   When saving the file, you should save it using the class name (Remember Java is case sensitive) and append ‘.java’ to the end of the name (if the file name and the class name do not match your program will not compile). Example : Assume ‘MyFirstJavaProgram’ is the class name, then the file should be saved as ‘MyFirstJavaProgram.java’.
  • public static void main(String args[]) – Java program processing starts from the main() method, which is a mandatory part of every Java program.

Java Keywords:

how to compile and run java program - My first Java Program [howpk.com]

how to compile and run java program – My first Java Program [howpk.com]

Comments in Java:

public class MyFirstJavaProgram{

/* This is my first java program.
* This will print ‘Hello World’ as the output
* This is an example of multi-line comments.
*/     public static void main(String[]args){

// This is an example of single line comment
/* This is also an example of single line comment.*/
System.out.println(“Hello World”);
}
}

Final Words:

Programming in Java is very easy. If you have little bit knowledge about OOP then you can easily obtain its syntax. Java is almost same like C++ programming. I hope now you learnt how to compile and run java program. As a assignment, do some practice of this program on you Machine and make few more experiments. In next class we will go more deep and play with arrays, stack and some complex programming logics. So, stay updated with me.

Author: Tanvir Zafar

Tanvir Zafar is a internet Entrepreneur and owner of this site and many others as well. He is student in GCUF doing BS Software Engineering. :)