Writing, compiling and executing first Java program.

In this topic, we will learn below topics.

  1. Writing first Java program
  2. Compiling the program using IDE, javac
  3. Executing the program using IDE, java
  4. Debugging the program
  5. Looking at output

Here is the simple Java class – Basic.Java

package simple;

 * Created by Sagar on 16-04-2016.
public class Basic {
    public static void main(String [] args){
        System.out.println("Hello www.softpost.org");

Compiling the Java class.
To compile the Java class file, you need to execute below command.

javac simple/Basic.java

Above command creates class file called Basic.class.

Running the Java class

To run the Basic.class file, you need to execute below command. Notice that you should be in the parent directory of simple (package directory) when running below command. Also note that how we access the class name. Since the class is in simple package, you need to access it using simple.Basic

Another important thing to note is that -cp switch. It tells JVM that classes can be found in current directory. If you do not pass correct class path, you will get error saying – could not find or load main class simple.Basic

java -cp . simple.Basic

Here is the output of the code
Hello www.softpost.org

Debugging the Java source code

Different IDEs provide different ways to debug the code using breakpoints. For example – Intellij IDEA allows us to add breakpoints, step into, step out of the break point.


You may also like...