August 2009


Note: JVM is Java Virtual Machine. It executes code that Java compiler generates. JVM does not know java programming language , instead JVM understands the machine instructions (bytecode) in the .class file. JVM is platform independent as it reads the bytecode produced by the compiler.

Sun Microsystems has implemented Java as ‘WORA’ ( Write once,run anywhere). I read somewhere that this implementation in reality is not easy as the slogan WORA is. People have often ridiculed it as ‘Write once, debug everywhere’ instead! ( Noooo, That comment is not going to stop me from being passionate about this language).

What is JVM?
Java language is compiled by the compiler to produce bytecode. This java bytecode executes on a special type of micro-processor. But, there was no hardware implementation of this micro-processor available when Java was first released. Hence, they came up with a virtual machine to emulate the micro-processor. So, ‘JVM’ actually resides on a host machine.
java08

 

 

 

 

 

Then, what is JRE?
JRE is Java Runtime Environment. JRE comprises of JVM and base Java API classes. JRE is operating-system and CPU specific meaning that each Operating system and processor will have different JRE installations. Thus, Java is truly platform independent with the ability that programmers can use the same source code on a variety of platforms.

java09

As usual, the first program good for a start is the HelloWorld example. Create a working directory ‘jsource’ and a sub- directory ‘chapter01’ .

java01
This will create and open a file called HelloWorld.java. Type the following code in it.

java03

The public class defined above is HelloWorld. The filename should be same as the public class. This also means that there can be only one public class in the file and there can be more than one private or protected classes in the file. The next step is to convert the programmer readable source file into instructions called bytecodes that the Java virtual machine will understand so that this code can run independently in any platform.

java02

On successful compilation of the source code one or more .class files will be produced. Each class in the source code will have a seperate .class file listed.

 java04

Note that there are two files HelloWorld.java and its class file HelloWorld.class listed above.  Let’s try running the program nowjava05

We have encountered an exception in the code with unsupported major minor version. But, we had compiled the code successfully. Ok, what’s the version of the java compiler used?

java06

That’s right. We are using JDK 1.6 with update 14. Now, what is the version of java runtime used?

java07

That’ s the culprit! We have different versions for the java compiler and the java runtime. We need to get this fixed. This error is popularly known as  ‘Unsupported major.minor version’

This error dawns when we try to run a class compiled in newer version with an older version of JVM. The following may be tried to fix this issue.

1) Open command prompt and type regedit. In tHe registry editor navigate to HKEY_LOCAL_MACHINE->SOFTWARE->JavaSoft->HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment. Click on this and check the value of CurrentVersion on the right hand side. Change this value to 1.6 as the value should be 1.6 if  JDK 1.6 or JRE6 is installed. Under the key HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment check if you find folders for 1.6. If yes the installation is correct. Type java -version in the command prompt and check if the version is 1.6 now.

2) Start->Control Panel->System-> Advanced->Environment variables. Check the value of path under system variables. Remove all references to previous versions of JDk and edit this to include C:\Program Files\Java\jdk1.6.0_14\bin if you have installed JDK1.6 under C:\Program Files.

3) If JRE6 is not installed correctly, then try reinstalling JDK1.6 kit or just JRE6.

The above solutions should help resolve the error as mine got resolved.Typing java -version should get you the below result now.

java10

Try to run the HelloWorld program again

java11

 We now have the java.lang.NoClassDefFoundError. This is because java.exe cannot find the .class files to interpret and execute. So, it is important to check if  the .class files are actually located in the directory that we want it to be. If the .class files re present then it is time to set the classpath variable.

Classpath is an environment variable that tells the java compiler javac.exe where to look for class files to import or java.exe where to fnd class files to interpret. 

Try java -classpath . HelloWorld now.
java12

Yes, it worked. But we had to specify classpath in the command line.I somehow do not like it at this point. Let’s try setting the classpath variable instead. Go to Start->Control Panel->System->Advanced->Environment variables. Under system variables select New. In variable name type classpath and variable value a dot (.) This should do the trick. Now close the command prompt and reopen it again. Try java HelloWorld now. Any luck?

java13

 

 

 

Phew, that was a long way…Hmm…how will the rest of my journey be if Helloworld was soo complex (complex ..lol…i know java experts are laughing at me now…but I will not stop!!!)