Whenever we start to learn a programming language, the first program is always to print the Hello World. In the last article, we learned how to install Java on Windows 10. Now we are ready to write and run our first Hello World Java program.
To keep things simple and working for a new user, here is the sample hello world program that you can use.
public class JavaHelloWorldProgram {
public static void main(String args[]){
System.out.println("Hello World");
}
}
Save above program as JavaHelloWorldProgram.java
in any directory.
Open Command Prompt and go to the directory where the hello world program file is saved. Then execute the below commands in order.
$javac JavaHelloWorldProgram.java
$java JavaHelloWorldProgram
Hello World
If you are using Java 11 or higher, then you can simply execute java JavaHelloWorldProgram.java
and it will compile and execute the program for you. No need to explicitly compile and then run the java program.
JavaHelloWorldProgram.java
Class_Name.class
extension. If you look at the directory where we compiled the java file, you will notice a new file created JavaHelloWorldProgram.class
Exception in thread "main" java.lang.NoSuchMethodError: main
.I have recently created a short video for Java Hello World Program using Notepad and then Eclipse. Watch it for a better understanding. https://www.youtube.com/watch?v=ZREpFyjTDho That’s all for this post and you can start playing with your first class. In the next post, I will get into further details of classes, JDK, JVM, and other features provided by the Java programming language. Update: Read this post to know about JDK vs JRE vs JVM in java.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Excellent… But i have a question… If a public class is present in any java source file…why we should save the file name with the public class name??
- PRAVEEN KUMAR BADAM
Thank you for amazing tutorials! Is it a good idea to add “how to create .exe / DMG / pkg” for this Hello World tutorial?
- Nik
public class ThankYou{ public static void main(String [] args){ System.out.println(“Thank you very much a good start form a beginner”); } }
- Adam
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.