Saturday, September 1, 2007

CD1 - Compiling a java program

Concept Discussion 1: Compiling a java program

It's the black box or the magician which converts your file which has the System.out.print("hello"); statement you typed to a program which prints "hello". So, we are going to type our java programs in any text editor you like - Notepad or Wordpad or Vi or Emacs (the later two are popular text editors in the Linux operating system). And, then we are going to use our compiler or the black box i told you earlier about and do that magic thing called compilation that will process the program that we have written and then generate a new file called the class file. This is then fed to what is called the Java interpreter - a program which understands and processes these class files and then generates the machine instructions (the language of only 1 and 0 which our computer understands) for doing the particular task. Ok, was that too much to digest? Let's do a quick recap. Getting your program running involves the following steps:

1. Type it out it in your favourite text editor.
2. Feed in your typed file to the Java compiler to generate the class file.
3. Feed this class file to the Java interpreter to see your actual program in action.


So, for steps 2 and 3 above, you need the java compiler and java interpreter in your machine. To know whether you have any of these already ( there is a good chance that you have the java interpreter if you browse a lot and have used a lot of applets on the web), follow steps below depending on whether you are a windows or Linux user.

For Windows users
1. Go to Start>Run and type "cmd" and click "OK".
2. In the black window tat appears, type "java" and hit the enter key.
3. If you a get a long list of options, you have the Java Interpreter or the Java Virtual Machine installed in your computer.
4. If you get back a message like 'java' is not recognized as an internal or external command, you probably don't have the Java Interpreter or the Java Virtual Machine.
5. Repeat steps 1 and 2. But hits time type "javac" instead of "java" in step 2.
6. If you get a list like in step 3, you have the Java compiler as well in your system.
7. If you get a message as in step 4, you do not have the Java Compiler in your system as well.

For Linux users:
1. Open a terminal (an XTerm) and type java
2. If you a get a long list of options, you have the Java Interpreter or the Java Virtual Machine installed in your computer.
3. If your shell says 'java:command not found ', you do not have the java compiler.
4. Repeat step 1. But hits time type "javac" instead of "java" .
5. If you get a list like in step 3, you have the Java compiler as well in your system.
6. If you get a message as in step 4, you do not have the Java Compiler in your system as well.

And before we go and check the results of what you tried above, i need to tell you one fundamental thing here. There were two important files, about which we discussed in Concept Discussion 1. From here on I will refer to concept discussions with CD (in bold). So the three files in CD1 were:

1. Your file typed in your favourite text editor with the java commands.
2. Your class file generated by the compiler.

You call the file in 1 as your java source code or simply java code or java source. And the file in 2 as your java program or java class.

So what do you do if do not have either of the Java compiler of the Java Interpreter (Java Virtual Machine)? You need to get it. How can you get either or both of them? Before answering these questions, you need to know a basic difference between the compiler and interpreter.

Definition
The compiler for any language is a program which translates programs written in that language to a from which can be executable binary machine language or some intermediate "ready-to-interpret" language or even another language. Don't worry if you have not understood this sentence. Keep reading.

Definition
The interpreter is the program which takes the output of the compiler if it's in the "ready-to-interpret" form (as said in the definition of a compiler above) and interprets it to machine language form and executes it on the target machine on which it is run.

In case of java, we have the following sequence:

java source -> java compiler -> class file containing bytecode -> java interpreter -> machine language.

This should make it clear to you that to actually execute java programs on your machine, you need to compile them and then execute the compiled bytecode using the java interpreter. So you actually need two programs. The compiler program is actually called "javac" and the java interpreter is "java" (nicknamed by many as the "Java Virtual Machine" or "JRE" or "Java Runtime"). The compiled bytecode is actually called as a class file.

If you have not understood the difference between the compiler and the interpreter from the discussion we had above, it's high time you understood that before going any further. So your tools to start with java are:
1. Your favourite text editor for writing your java programs.
2. A java compiler to compile programs that you write using the text editor in step 1 above
3. The java interpreter to interpret and run the bytecode or class files that you generate using the compiler in 2 above.

Item 1 above can be anything from your notepad program in windows or Vi or Emacs in Linux. You'd definitely have it if you had an operating system of any kind. The easiest way to get the compiler and interpreter (items 2 and 3 above) is to download the JDK or J2SE, which is basically the Java Development Kit which comes with both the compiler and the Virtual Machine. Beware if someone tells you the JRE has everything. It doesn't . It has just the interpreter - that means you can run class files you may have with you, but you cannot compile your java programs. This explains why people having the JRE are able to run java applets (because they are just class files) but not compile java programs.

For everything in the world there are rules. So is the case with Java. You always need to name your file which you write in Notepad with a .java extension. If you don't know what an extension is, let me tell you that filenames which you see commonly have two parts - for eg: myexpenses.txt . This is a text file. How did I know that without seeing the file content ? I knew that because of the file extension. The extension is that part of the filename which comes after the dot - in the example, that was "txt". So if i need to name a file with java extension, i would name it as myprogram.java. There are very commonly used extensions like mp3. So the song "Nothing else Matters" on your computer might probably have the filename "Nothing Else Matters.mp3". We can say that an extension of a file helps it to be classified as a file of a particular type. So an extension of "java" classifies it as a text file containing a java program.

No comments: