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
- 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); } }
- Run below command in dos window to compile and run the java file.
cd C:\WorkSpace\dev2qa.com javac JavaHelloWorldExample.java java JavaHelloWorldExample
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.
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.
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.
- Click ” File —> New —> Others” in eclipse menu bar.
- Input Java in the popup dialog search text box and choose Class.
- Click “Browse” button to choose the java file source folder. Input class name and select the main(String args[]) method checkbox.
- Click Finish button and input below code in the main method.
String str = "Hello World"; System.out.println(str);
- Right click the file, Click ” Run As —> Java Application ” to run it.
- Right click the first column in any line, Click ” Toggle Breakpoint ” to enable or disable a break point in source code.
- Right click the file, Click ” Debug As —> Java Application ” to debug it.
- When in debugging mode, click the top tool bar item to perform restart, stop, step into, step over actions.
- The output will be shown in Console view at eclipse bottom.
Create The Runnable Jar File
- Right click the java project. Click ” Export ” in the popup menu.
- Choose ” Runnable JAR file ” item in the popup dialog, click Next button.
- Choose the HelloWorld java class, click Browse button to set the Exported jar file name and location.
- 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.
Download “JavaHelloWorldExample.zip” JavaHelloWorldExample.zip – Downloaded 86 times – 336 B