Thursday, November 14, 2013

#3 JDK and JRE. Father and Son..!

Hello,

In this post, you are going to learn the difference between JDK and JRE which plays a major roles in Java Language.

If you have successfully installed Java in your system, then you should have Java folder in your installation path. Generally all the software’s will be installed in C:\Program Files directory (for Windows Operating Systems). If you go inside Java folder, you can see 2 folders.

  1.         jdk xxxxx
  2.         jre xxxxx                              where xxxxx is your Java version.
    
   Let us discuss about these 2 folders.


   JDK stands for Java Development Kit. As the name suggests, this folder contains files which helps the user to develop Java based applications. This folder is just like a tool kit for building java applications. If you go inside jdk xxxxx folder, you can see bin, lib, jre and other folders. There is nothing much to talk about other folders, but we need to talk about bin, lib  and jre folders.
  1.         bin – this folder contains all the executable files which are useful for compiling and running the java programs. 
  2.     .    lib – this folder contains jar files that are required for the executable files in the bin folder.
  3. .       jre – I have explained below about this folder in detail.

    
    JRE stands for Java Runtime Environment. As the name suggests, if we want to run a java program you just need JRE folder, but if you want to compile Java program you need JDK folder. If you have observed, JRE folder exists inside JDK folder and also outside JDK folder. So we can say that, JDK is the super-set of JRE. If you delete your JRE folder which is outside JDK folder, you won’t get any issues while compiling/running java programs as you already have JRE folder inside JDK.
Let us learn how to compose/compile/run a Java program.
Create a text file LindlarsForLife.java inside C:\Program Files\Java\jdkxxxx\bin folder. Copy below content, paste it in the text file and save the file with .java extension.

public class LindlarsForLife {
                public static void main(String[] args) {
                                System.out.println("Hello..! Welcome to LindlarsForLife..!!");
                }
}

Now open the command prompt (Windows+R, type cmd and hit enter) and follow the below steps.
  1.         You need to go to the java bin directory for identifying java commands.
    For example, I have java 1.5.0_22 version
    C:\>  cd C:\Program Files\Java\jdk1.5.0_22\bin
    After hitting enter, the above command will take you to the bin directory where you have your LindlarsForLife.java file. Your command prompt will change as shown below.
    C:\Program Files\Java\jdk1.5.0_22\bin>
    2.       For compiling LindlarsForLife.java file, we need type as shown below,
    C:\Program Files\Java\jdk1.5.0_22\bin>javac LindlarsForLife.java
    The javac command is used for compiling a java file. When you hit enter for the above command, if you don’t have any errors/exceptions in your program you get a blank command prompt as shown below.
    C:\Program Files\Java\jdk1.5.0_22\bin>
    3.       At this point, if you go and check your bin directory for .class file you can find LindlarsForLife.class.  So we can say that java has compiled our java program and generated .class file.
    *Remember this point, I will be using this .class file again below..!
    4.       Now we need to run this .class file. We can do so by using the following command.
    C:\Program Files\Java\jdk1.5.0_22\bin>java LindlarsForLife
    If you observe, this time we are just writing java instead of javac and we are not mentioning .java extension for the file name. This is because we are trying to run the .class file. The java command will search for .class files in the particular folder and will compare it with the name you have mentioned (here we have mentioned LindlarsForLife). But for javac command we need to explicitly mention the type of file to compile.
    So when we hit enter for the above command, the desired output will be displayed. In our case, the output will be
    Hello..! Welcome to LindlarsForLife..!!

For more clarity, you can see my command prompt how I had executed the commands for compiling and running the LindlarsFoLife.java program


If you get the above output, then CONGRATS.! You have successfully run your first Java Program. :D

If you get any error, then also CONGRATS.! You have successfully got your first Java Program Error. :P So try again by following the above procedure correctly for getting the expected output.

Till now we have been playing inside JDK\bin folder, but now let us play with JRE. We will do the same above procedure with JRE folder and will see what happens.
Copy the LindlarsForLife.java inside JRE\bin folder. Make sure, you copy the file to JRE\bin that is outside JDK folder.

        1.    Now again open the command prompt and go to the jre\bin folder
     C:\Program Files\Java\jdk1.5.0_22\bin>cd C:\ProgramFiles\Java\jre1.5.0_22\bin
             Hit Enter, we get below command prompt.
             C:\Program Files\Java\jre1.5.0_22\bin>
        2.    Let us compile LindlarsforLife.java file.
             C:\Program Files\Java\jre1.5.0_22\bin>javac LindlarForLife.java
             When we hit enter, we get an error saying
              'javac' is not recognized as an internal or external command,
              operable program or batch file.

Why we get an error..?
Because JRE doesn’t understand what is javac command. In more detail, JRE\bin doesn’t contain javac.exe file which is used for compiling and hence it says not recognized command.

From the above experiment we got to know that, JRE cannot compile a java program.
Now let us see if JRE can run a java programming. For testing so, we need a .class file. Yes, I know you guys remember that we have LindlarsForLife.class file inside JDK\bin folder. So let us and copy/paste it in JRE\bin folder.
Let us try to run and see what happens.
C:\Program Files\Java\jre1.5.0_22\bin>java LindlarsForLife
When I hit enter, I get the desired output.
Hello..! Welcome to LindlarsForLife..!

From this experiment we learnt that, JRE can only run .class files.

So the bottom line is -- if we want to compose and compile a java program, we need JDK. JRE alone cannot do that. But if we want to run a java program (.class file), both JDK and JRE can do that..!!

Hit Points:

  1.     JDK is the super set of JRE.
  2.     JDK can compile and run java programs.
  3.     JRE can only run java programs. It cannot compile.

   Thanks. :D

0 comments:

Post a Comment