Core Java - Start it Now!

You need the following to start with Java:

1. Text Editor


Note: you can use any other Text Editor, e.g. Notepad, you are comfortable with.
2. JDK (Java Development Kit)
        Get JDK 1.6


OR


Download the complete solution i.e. Java 6 Update 26 with NetBeans 7.0

Now, Let's start with Core Java: A Simple Program, shows "This is my first java program".

Open your text editor and write the given lines of Code:
class MyClass
{
public static void main(String args[])
{
System.out.println("This is my first java program");
}
}
Save your file as "MyClass.java" exactly to folder "bin" (find "bin" in the installation directory, you provide at the time of installation of jdk). Consider the class name in the code above i.e. "MyClass" which contains the main function. The program is executed by calling main(). So, the name of the file should be the name of the class containing main() and the extension must be .java otherwise you'll not be able to run the program.

One more thing, Java is case sensitive, the class name "MyClass" is not similar to "myclass", "MYCLASS", Myclass, etc.

Open Command Prompt

Start Menu >> Run. Type "cmd" and press enter.

CJStartHere1.gif

Go to "bin" directory and compile the program by executing the Java compiler "javac" as shown in the following image:

CJStartHere2.gif
Compiler creates a "MyClass.class" which contains the bytecode version of the program. You can find "MyClass.class" in the "bin" folder
CJStartHere4.gif
Now, to run the program, use Java application launcher "java" and pass the name of class "MyClass" as command line argument, as shown in the following image
CJStartHere3.gif
You can see the output of the program in the image above.
Why, save the file in "bin" directory

Now, you know that you must use Java complier (javac) and Java application launcher (java) to run a program. These both files are resided in the bin folder, so that, we need to save the .java file to the bin directory.

What to do, if  don't save file in "bin" directory
   
Yes, you can save your file anywhere in the storage area, but you need to set path to the bin directory as shown in the following image
CJStartHere5.gif

Consider the image above, I save "MyClass.java" file to "d:\java", so I need to set the path to the "bin" directory which is resided in "c:\java\jdk1.6", so I used the following command to set the path

>>set path=%path%;c:\java\jdk1.6\bin;.;
OR, you can set path permanently to run the program whether it is saved in any directory. To set path permanently in windows, follow the steps given below:
  • Right-click on My Computer
  • Select Properties
  • Click on Advanced Tab
  • Press Environment Variables
  • Select "Path" from the list, under "System variables"
  • Press Edit
  • Go to the end of "Variable value", insert a semicolon (;) to separate the path and write the path to "bin", for e.g. "c:\program files\jdk1.6.26\bin"
  • Press OK.

Running the same program in NetBeans 7
Open NetBeans -> File Menu -> New Project
 CJStartHere6.gif.jpg
Choose Java and Java Application under Categories and Projects respectively and press Next button
 CJStartHere7.gif.jpg
Now, give the project name and location and press Finish button, you'll see that a class "MyClassNetBeans" containing the main() is created. You just insert a line of code i.e. "System.out.println("This is my first java program");" in the main(), as shown in the following image:
 CJStartHere8.gif.jpg
Press "F6" to run the program.

Output:

CJStartHere9.gif

I hope that you have chosen a solution (either Text Editor & JDK or NetBeans with JDK) to start with Core Java.

If you encounter any difficulty, ask it here ...

Up Next
    Ebook Download
    View all
    Learn
    View all