Thursday, November 10, 2011

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 specially 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 text book 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 was already setup but when I tried the same thing in my home Windows PC I had 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 command prompt

Step by Step Guide to Run Java Program in PC


1. Download JDK from Oracle.
first step is downloading correct version of JDK and correct installer for your machine. Since Java is supported for multiple platform you see lot of installer available for JDK. if you are running on windows PC than you should download Windows Installer of 32 bit machine since most of Windows desktop and laptop are 32 bit and until you know that its for Windows Server which can be 64 bit. If you are running on RedHat linux or Ubuntu than you can also download tar file.


2.Second step is to installing Java
If you are running on Windows PC than installing Java is just a cakewalk. just double click on Installer and it will install Java in your machine. it usually creates a folder under program files/Java/JDK_version , this folder is important because in many script 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 its 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. 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 filr 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 it reside on some other directory you may not be able to compile until you explicitly provide classpaht 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. Seventh and final step is to run 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 upto this state then you can also try How to debug a Java program


Related Java Tutorial

15 comments :

Anonymous said...

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.

Anonymous said...

good tutorials..

For javascript and basic java tutorials.. visit

http://anurag-tutorial.blogspot.com/

thanks

Anonymous said...

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

Heidarzadeh said...

Command line and command prompt are the same.

John said...

Can 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?

Andrew said...

Can 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?

Anonymous said...

command line arguments are those which you pass while running your Java program from command prompt using "java" command e.g.

java Helloworld abcd 1234

will run HelloWorld class and pass two command line parameters.

ClintJCL said...

This isn't going to work because you opened your string with " but closed it with '

ClintJCL said...

Also, I got this error:

Exception 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 :/

Anonymous said...

i am having some problems with running the java program i created with command prompt can anyone help me ?

Anonymous said...

Can you please write about

How 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

Anonymous said...

Best HelloWorld guide for Java Developers, I have seen so far. Thanks a ton dude.

Anonymous said...

I 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?

Neel said...

what if ..m not able to save my program in bin?
means 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

Anonymous said...

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.
Whenever I typed it into command prompt, it then said the file couldn't be found.

Post a Comment