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 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 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 gets 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 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.
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.
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 a 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 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.
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
step by step tutorial, you better call it HelloWorld Example in Java, it would be more appropriated. I wanted to write HelloWorld in Java and found this quite helpful.
ReplyDeletegood tutorials..
ReplyDeleteFor javascript and basic java tutorials.. visit
http://anurag-tutorial.blogspot.com/
thanks
I was looking for How to run java program from command line and got this , I have a confusion is command prompt and command line is same thing or different. java on command line or java on command prompt will run alike or not, please advise
ReplyDeleteCommand line and command prompt are the same.
ReplyDeleteCan I run my Java program written in notepad or using DOS Editor using above steps ? I understand I need to save by Java program to .java file, compile it using javac command and run it using java command but my doubt is I am coding in windows using notepad but I need to run my Java program in Solaris host which is a Unix machine, I heard Java program written in notepad in windows contains line separator as /r/n while line separator in Unix is /n, won't that create any issue while running Java program in Solaris box?
ReplyDeleteCan you please let us know How to run Java program in any IDE like Netbeans or Eclipse, I have coding in text editor as textpad or notepad and don't like writing Java program in VI or EMACS editor in Unix operating system e.g. Linux, AIX, BSD or even Solaris. What I want to learn is using Eclipse to run my Java program as soon as I finished coding, is it possible to do that without setting path or classpath etc?
ReplyDeletecommand line arguments are those which you pass while running your Java program from command prompt using "java" command e.g.
ReplyDeletejava Helloworld abcd 1234
will run HelloWorld class and pass two command line parameters.
This isn't going to work because you opened your string with " but closed it with '
ReplyDeleteAlso, I got this error:
ReplyDeleteException in thread "main" java.lang.NoClassDefFoundError: helloWorld
Caused by: java.lang.ClassNotFoundException: helloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: helloWorld. Program will exit.
Of course, my classpath is just fine, and matches what Eclipse is successfully using... But I really wanted to try something at the command line :/
i am having some problems with running the java program i created with command prompt can anyone help me ?
ReplyDeleteCan you please write about
ReplyDeleteHow to run Java program from Windows 7 and Windows 8 ?
How to run Java program from UNIX, Linux and Solaris ?
I heard Java is platform independent but still I have trouble running Java program from Windows to Unix, please help
Best HelloWorld guide for Java Developers, I have seen so far. Thanks a ton dude.
ReplyDeleteI have read this tutorial. But i have an issue while i am saving the notepad file demo.java in D drive. Now how to compile by command prompt?
ReplyDeletewhat if ..m not able to save my program in bin?
ReplyDeletemeans m getting a dialogue box saying that u don not have authority to access it ..wuld u like to save in my documents ? wat shuld i do ? plz help
What program should it be typed into? I was using notepad but the .java part wasn't saving as part of the name for some reason.
ReplyDeleteWhenever I typed it into command prompt, it then said the file couldn't be found.
I am answering a couple questions asked by readers. (1) If using Notepad, when saving, on "Save as Type:" select "All files (*.*)", and make sure the name of the file is exactly the same as the class name. In the above case, the file name must be "Helloworld.java". (2) There is an error in the example file! The text in the line should read as follows: System.out.println("I am running my first Java program"); Note the closing " mark instead of '. (3) To open a command prompt in Windows 8, easiest (I think) is to right-click the Start icon (lower left corner) and select it from the choices. Then, navigate to where you have saved the Helloworld.java file. Perhaps this will be in your desktop or documents folders? The command to change directories is cd. The command prompt program usually starts you at c:\Users\yourname. If the file is in the Documents directory, type "cd Documents". If you type "dir" it will list all files and directories in your current file location.
ReplyDeleteWhen i try to run my test.java program (which compiles perfectly fine) i get the error, that 'the file C:\ProgramData\Oracle\Java\javapath\java.exe cannot be found'. I couldn't find any solution for this problem on the Internet, do you know what could cause the problem and how to fix it?
ReplyDeleteplz tell me i m facing an error while running helloworld program the error is
ReplyDeleteerror while writing HelloWorld :HelloWorld.class(Access is denied)
public class Helloworld
^
1 error
Hello. My version of Java is jre1.8.0_40 I'm confused about what to install for the Path, would there be a conflict if I were to install a JDK to my version of Java? I saw something confusing at Oracle when I was about to download the said instructions above. It says that JDK (Download), Server JRE (Download) & JRE (Download). What should I do now?
ReplyDelete@Anonymous, you need JDK if you want to do Java development e.g. writing Java code, compiling them to class file and run them using java command, but if you have a JAR file and you want to run it, JRE is enough, same is the case with Applet.
ReplyDeleteSo, download and install JDK if you want to write Java code. You should define a JAVA_HOME variable and add %JAVA_HOME%/bin in PATH so that javac and other tool are available to your command prompt.
See following tutorials to learn more :
How to set JAVA_HOME see here