Sabtu, 15 Maret 2014

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


































Source:http://javarevisited.blogspot.com/2011/11/run-java-program-from-command-prompt.html

Tidak ada komentar:

Posting Komentar