Helloworld Example Java : How to run Java Program from command prompt
This article contains HelloWorld Example in Java and step by step guide to run Java program from command prompt. Beginners who just started to learn Java or using Java often struggled and doesn't know how to run a Java program from command prompt. Running a Java program is simple but setting up Java environment is rather cumbersome especially if you are new in Programming world and not very familiar with words like PATH, CLASSPATH or even command prompt.
I don't blame you because we all start at some point of time and even When I started to wrote my first Java program, What I was doing was simply typing HelloWorld from a textbook in a notepad editor to my surprise my Java program ran without any issue because I was running that on school PC where PATH and CLASSPATH were already setups but when I tried the same thing on my home Windows PC I had a tough time to run my Java program including HelloWorld Example in Java.
I was thinking of this article when I wrote How to Convert String to Integer in Java and How to convert String to Double in Java but somehow it get delayed. Anyway, now I am happy to put these steps in an article.
I also started with HelloWorld in Java but After programming in Java for few years we all know more than that and subtle details about Java programming language but there are still many people who are learning Java programming and they often face the same problem which We have already solved.
In This article, I will put a step by step solution to run a simple Java Program and Setup Java programming environment from scratch to help beginners who are trying to run Java program including popular Example of HelloWorld in Java.
Running Java Program from the command prompt
Here is the Step by Step Guide to Run Java Program from the command line:1. Download JDK from Oracle.
the first step is downloading the correct version of JDK and correct installer for your machine. Since Java is supported for multiple platforms you see a lot of installers available for JDK. if you are running on windows PC then you should download Windows Installer of 32-bit machine since most of Windows desktop and laptop are 32 bit and until you know that it's for Windows Server which can be 64 bit. If you are running on RedHat linux or Ubuntu then you can also download tar file.
2. The second step is to install Java
If you are running on Windows PC then installing Java is just a cakewalk. just double click on Installer and it will install Java on your machine.
It usually creates a folder under program files/Java/JDK_version , this folder is important because in many scripts this is refereed as JAVA_HOME and we will specify an environment variable JAVA_HOME pointing to this folder.
If you are running in Linux or any Unix machine including AIX, Solaris etc. you just need to extract tar file and it will put all the binary in a folder this will be your JAVA_HOME in Linux.
3. Third Step is Setting PATH for Java.
for setting PATH you just need to append JAVA_HOME/bin in PATH environment variable. For step by step guide and details How to Set PATH for Java in Windows, Linux, and Unix.
4. Testing Java PATH
Before you run your first Java program it's better to test PATH for Java. Now open a command prompt in Windows just go to "Run" and type "cmd". Now type "java" or "javac" if you see large output means Java is in PATH and you are ready to execute Java program.
5. The fifth step is to write your first Java program
Best program to quickly test anything is HelloWorld , just type below program and save it into file called HelloWorld.java
public class Helloworld{
public static void main(String args[]){
System.out.println("I am running my first Java program');
}
}
Java starts execution from main function just like in C.
6. Sixth Step is to compile Java program
For compiling just type javac HelloWorld.java, it will compile this Java file and create a corresponding .class file for execution. Java finds classes by looking on CLASSPATH , by default Classpath points to "." current directory and that's why we are able to compile our Java Class.
It reside on some other directory you may not be able to compile until you explicitly provide classpath by option –cp or specify that on Environment variable called CLASSPATH. for more details see How to Set ClassPATH in JAVA on Windows, Linux, and Unix .
7. The seventh and final step is to run the Java Program.
Now we are ready to run our first Java program. just type "java HelloWorld" and it will execute HelloWorld class and execute its main method which will print "I am running my first Java program".
Now you know how to run Java program in your own PC in just seven steps. If you face any error or issue , please let me know and I will try to help you.
That's all on this HelloWorld Example in Java and How to run Java program from command prompt, also If you have any JAR file for execution than you cal also run it by giving command java -jar HelloWorld here Helloworld must be defined as Main-Class inside Manifest file of JAR. If you have reached up to this state then you can also try How to debug a Java program
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
Related Java Tutorial
How to deal with OutOfMemoryError in Java
How to resolve ClassNotFoundException in Java
How to use Comparator and Comparable in Java with Example
How to Override Equals in Java
How to implement hashCode in Java
How to Create Thread in Java
How to Setup Java Remote debugging in Eclipse
Join the conversation