This post would teach how to compile and run a simple Java program from command prompt. Simply follow following steps in sequence -if any issues fire them at me.
STEP 1: Install jdk1.6 (latest version) on your system. (If any issues in installation, do ask here). Look at the Download JDK from here for downloads.
STEP 2: Create a source folder in your system where all your Java programs will be stored (let it be C:\JavaSrcCode..This is my system made folder. I would recommed you all to make following folder structure D:\MyJava\source)
STEP 3: Create Example.java file in the above created folder(My is JavaSrcCode, your will be 'source' folder). Example.java will have following lines of code in it.
a) Switch your location to bin directory of installed location of your java(C:\Program Files\Java\jdk1.6.0_02\bin)
b) Now from that location type "javac
See following figure
Above command successfully generates the Example.class file for Example.java file in C:\JavaSrcCode folder only where Example.java file is stored.
STEP 5: Now for running the class file. Enter the following command from command prompt
java -classpath e.g. "
-classpath : is just nothing but the list of directories in which classes might be found e.g in above case C:\JavaSrcCode is used to tell the JVM to look for .class file in specified folder. While running the file , there is no need to give Example.java (because while running JVM takes .class file as input -not .java file)
So above commands in the diagram will successfully run the program showing the output.
[ Note: There is one more simplest method of making compiling and running of Java programs easier. Don't need to make Example.java file in any folder. Simply make it in bin folder of installed java in your system.
Then while compiling the Example.java,
a) Switch your location to bin directory of installed location of your java(C:\Program Files\Java\jdk1.6.0_02\bin)
b) command will be simply javac Example.java
and while running, command will be simple java Example
But we should avoid using this approach of using Java as it is not good to maintain your project structure consisting of Java files in installation directory.
]
[ Note: There is one more simplest method of making compiling and running of Java programs easier. Don't need to make Example.java file in any folder. Simply make it in bin folder of installed java in your system.
Then while compiling the Example.java,
a) Switch your location to bin directory of installed location of your java(C:\Program Files\Java\jdk1.6.0_02\bin)
b) command will be simply javac Example.java
and while running, command will be simple java Example
But we should avoid using this approach of using Java as it is not good to maintain your project structure consisting of Java files in installation directory.
]
One more easier way is to use Java IDE. If any of you can get Netbeans version 6 or Eclipse(freeware) or Gel. Then it will be well and good. It will reduce your effort in running and compiling java programs through Command prompt.I have covered it in next post. Head over to it. Creating Simple Java Program in Netbeans IDE .
|
Thanks !
[ PS: Copied from Have Fun With Java :sshhh: ]
