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.

1. 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 the below command in the dos window to compile and run the java file.
    cd C:\WorkSpace\dev2qa.com
    
    javac JavaHelloWorldExample.java
    
    java JavaHelloWorldExample
  3. 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 when you compile it.
  4. When the compile process completes successfully. There will have a JavaHelloWorldExample.class file that exists in the same folder. You can use the java command to run the .class file. But remember the parameter for the java command is just the class name without the “.class” extension.
  5. You can find both the command javac and java in the 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.

2. Steps To Write Hello World Example With Eclipse.

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

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

3. Create The Runnable Jar File.

  1. Right-click the java project. Click ” Export” in the popup menu.
  2. Choose the ” Runnable JAR file ” item in the popup dialog, click the Next button.
  3. In the Runnable JAR File Export —> Runnable JAR File Specification dialog, Choose the JavaHelloWorldExampleYour-project-name in the Launch configuration: drop-down list.
  4. Click the Browse button after the Export destination: drop-down list to set the Exported jar file name and location.
  5. In the Library handling: section, check the Package required libraries into generated JAR radio button.
  6. Click the Finish button. When the process complete, you can find the jar file in C:\WorkSpace.
  7. Go to C:\WorsSpace and run the command java -jar Dev2qaExample.jar in a dos window to run the jar file.
  8. You can see the output Hello World in the dos window also.

Leave a Comment

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.