Wednesday, July 18, 2012

Installing Java in your computer

One second.. Before installing java in your computer make sure you don't have Java already installed.

Step 1: Check for Java in your computer :Simply double click My Computer and go to C->Program Files. If you can see a Java folder there, feel happy you may have Java installed. Still to make sure double click the Java folder and search for some jdk1.x.x [x is just a version number]. Double click it and there you can find a "bin" directory. Inside the bin directory search for "javac.exe" and "java.exe" files. If found really DONE!! javac.exe is the java compiler and java.exe is the java interpreter. So with them you can successfully run your program. Goto step3 directly

Step 2:  Install Java if not available in your computer : Type "http://www.oracle.com/technetwork/java/javase/downloads/index.html" in your browser.


Click download Java Platform (JDK). The following screen appears where you accept the license and click the jdk for your machine. I have windows - 32 bit OS. So I click on windows x86. [Note: windows x86 implies 32-bit OS]
The following window appears next:

Click Run . The following window appears:
The following window appears where you press run:

The below window appears:
Click Next button. Then the window shown below appears. You are going to develop java program. So make sure 'Development Tools' has been selected.
Click Next. The installation by default places the java inside your C:\Program Files. Now you go to my computer-> C-> Program Files, you can find it there. Done!!! Inside Java->jdk->bin, you can find the javac [java compiler] executable file and java[interpretor] executable file. So with these tools you can develop java program!!

Any doubts, you can ask me

Step 3: Create a folder into a drive you wish, say D:\; Let the folder name be myJavaCollection. Now lets start with the java program

Open notepad and type the following code there:
class Sample
{
     public static void main(String[] a){
         System.out.println("HELLO WORLD");
     }
}

Now save the notepad content. Click save and the name of the file should be entered as Sample.java. It is to be noted that the class name ( here Sample) and the name under which you save must be same. The .java file extension is also mandatory. The above program must be saved as Sample.java

Step 4: Now its time to compile and run your java code. As I told before, to compile a java program you need a java compiler. You can find this java compiler inside c:\program files\java\jdk1.x.x\bin. Inside bin can you find many .exe files. There you can see javac.exe and java.exe files. javac is used to compile your source code and it produces a class file [which is not an object file. it is just a bytecode]. java is the interpreter that you can use to execute your bytecode.

Now open the command prompt in your system. Run->cmd

Now change to the directory where you saved your program [here, Sample.java]. Suppose the directory is D, enter d: and press enter. You can see that the command prompt is changed to the d: drive. Now enter cd myJavaCollection and press enter.

Step 5: Now just type javac in your command prompt to find whether your OS could recognize the compiler. If it couldn't recognise, it displays an error message as shown below: 'javac' is not recognized.


Don't worry. Lets make the compiler to be visible to the Operating system. Assuming you are using Windows OS, follow the steps given below:
a)Right Click My Computer and select Properties


First click on the advanced tab and then on the Environment Variables.

The following window appears:
Click on the New button highlighted in the above figure. Then you get a small panel window as shown:
For the Variable name, give PATH.For the Variable value, give the path of your javac and java tools. In my example it is C:\Program Files\Java\jdk1.7.0\bin. Please, dont copy and paste this path. Go to your system drives and locate the of bin and paste there.
It is very IMPORTANT : Close the already opened cmd prompt window because the path set in the environmental variable wont be reflected in the already opened command prompt. Open another command prompt. Run-> cmd .

Now compile your java program using javac Sample.java
Then run your program java Sample

No comments:

Post a Comment