As usual, the first program good for a start is the HelloWorld example. Create a working directory ‘jsource’ and a sub- directory ‘chapter01′ .
This will create and open a file called HelloWorld.java. Type the following code in it.
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.
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.
Note that there are two files HelloWorld.java and its class file HelloWorld.class listed above. Let’s try running the program now
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?
That’s right. We are using JDK 1.6 with update 14. Now, what is the version of java runtime used?
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.
Try to run the HelloWorld program again
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.
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?
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!!!)