this is a simple tutorial on how to compile and run java programs, i made this coz i have seen many of beginners doesn't know how to compile and run..
First, we should have JDK installed on our PC.. any version will do as long as it is JDK.. you can download it from
http://developers.sun.com/downloads/new.jsp it's free.. after downloading, install it..
now, we have a JDK installed, we can now start to program.
1.
Coding the Java Program.. here's a very simple java program. the HELLOWORLD program.. (copy the code below and paste it on a notepad. save it as HelloWorld.java, make sure the classname and the filename must be the same, java is case sensitive. and make sure you saved it as in .java file extension and not in .txt)
- Code:
-
/**
* The HelloWorld class implements an application that
* simply prints \\"Hello World!\\" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println(\\"Hello World!\\"); // Display the string.
}
}
Notice the /**/ -- this is a block comment.. and the
// -- this is a line comment, the characters after // and along the line, will not display..
it's a great help if you put comments on your programs.. you'll not be confused when you are programming a huge java program..
2.
Compiling Java Program- click start >> run >>> type cmd.. then the command prompt should be there.
type in there, "cd C:\\Program Files\\Java\\jdk1.6.0_06\\bin" and press return(without quotes)
where C:\\Program Files\\Java\\jdk1.6.0_06\\bin is the directory path of my bin folder where my JDK is installed..
you will notice that from C:\\Documents and Settings\\onatlheen\\> will
become C:\\Program Files\\Java\\jdk1.6.0_06\\bin\\> [see figure below]
type in "javac C:\\java\\HelloWorld.java" [without quotes] and press return
*where C:\\java is the directory path of my java file.. and HelloWorld.java is the filename that i am compiling.
to know if it was compiled, a .class file with a same filename will create on the same folder where HelloWorld.java resides..
If you do not have the HelloWorld.class file, it was not compiled. repeat steps from the beginning CAREFULLY.. do not proceed on the next step, because you can't.. lol..
3.
Running the Java Program-on the command prompt, type in there "cd C:\\java" [without quotes] and press return
*where C:\\java is the directory path of my java program.
*to know the directory path of your java program, look at the address bar of the folder.
and finally type "java HelloWorld" [without quotes] and press return
you will now see the output of your program! [refer to the figure below]
SUMMARY for the Experienced in command prompt
1. cd [bin folder of the JDK installed]
2. javac [dir path of the file] [filename to compile]
3. cd [dir path of the file]
4. java [filename of java program file]
You may reply to this post through this thread ->
https://myonlinekr.forumotion.com/programming-f11/ if you have some questions.. I am not great in creating tutorials.. I'm only great in spamming forum boards..