Java Hello World Example

If you are a beginner of Java programming. You may want to know how to write the “Hello World” example in Java. Because it is the most simple and popular example for learning any programming language. But before doing that, you need to first Install Java Development Kit and set the java executable file in the system environment PATH variable.

Steps To Write Hello World Example In Command Line

  1. Input below code in a text editor. Save it to file C:\WorkSpace\dev2qa.com\JavaHelloWorldExample.java.
    public class JavaHelloWorldExample {
    
    	public static void main(String[] args) {
    		
    		String str = "Hello World";
    		
    		System.out.println(str);
    
    	}
    
    }
  2. Run below command in dos window to compile and run the java file.
    cd C:\WorkSpace\dev2qa.com
    
    javac JavaHelloWorldExample.java
    
    java JavaHelloWorldExample

    compile and run java class
    javac is used to compile the java source file to class file. The parameter is the file name with the “.java” extension. If there has any compile time error, the compiler will print out the error message in the console like below when you compile it.
    java compile error message
    When the compile process complete successfully. There will has a JavaHelloWorldExample.class file exist in the same folder. You can use java command to run the .class file. But remember the parameter for java command is just the class name without the “.class” extension.

    You can find both javac and java command in bin folder under your jdk installation directory such as %JAVA_HOME% / bin folder. In my environment it is C:\Java\jdk1.8.0_131\bin.
    java bin folder

Steps To Write Hello World Example With Eclipse

Before you can do it, you had better read Beginner’s guide for install jdk(java delvelopment kit) and eclipse in windows to learn how to setup Eclipse in windows.

  1. Click  ” File —> New —> Others” in eclipse menu bar.
    eclipse file new others menu item
  2. Input Java in the popup dialog search text box and choose Class.
    choose java class in popup dialog
  3. Click “Browse” button to choose the java file source folder. Input class name and select the main(String args[]) method checkbox.
    browse to select the source folder input java class name
  4. Click Finish button and input below code in the main method.
    		String str = "Hello World";
    		
    		System.out.println(str);
  5. Right click the file, Click ” Run As —> Java Application ” to run it.
  6. Right click the first column in any line, Click ” Toggle Breakpoint ” to enable or disable a break point in source code.
    set break point in java test method
  7. Right click the file, Click ” Debug As —> Java Application ” to debug it.
  8. When in debugging mode, click the top tool bar item to perform restart, stop,  step into, step over actions.
    java debug toolbar
  9. The output will be shown in Console view at eclipse bottom.
    eclipse console output

Create The Runnable Jar File

  1. Right click the java project. Click ” Export ” in the popup menu.
    java hello world export executable jar file
  2. Choose ” Runnable JAR file ” item in the popup dialog, click Next button.
    choose Runnable JAR files item
  3. Choose the HelloWorld java class, click Browse button to set the Exported jar file name and location.
    choose hello world java class
  4. Click Finish button. When the process complete, you can find the jar file in C:\WorkSpace. Go to C:\WorsSpace and run below command to run the jar file.
    run runnable jar file in command

Download “JavaHelloWorldExample.zip” JavaHelloWorldExample.zip – Downloaded 55 times – 336 B

(Visited 359 times, 1 visits today)
READ :   How To Install Spring IDE Eclipse Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.