How to setup Java Environment and run Java Program from command prompt - Helloworld Example (original) (raw)
This article contains HelloWorld Example in Java and step by step guide to run a Java program from the command prompt. Beginners who just started to learn Java or using Java often struggled and don't know how to run a Java program from the command prompt. Running a Java program is simple but setting up a Java environment is rather cumbersome especially if you are new in the 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 in 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 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 set up a Java programming environment from scratch to help beginners who are trying to run Java programs including the 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 and print Hello World:
1. Download JDK from Oracle.
the first step is downloading the correct version of JDK and the 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 Windows desktops and laptops is 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 the 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.
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 the 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 the main function just like in C.
6. Sixth Step is to compile the 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 resides 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 a Java program on 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 the command prompt, also If you have any JAR file for execution then 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
Related Java Tutorial