Tuesday, October 23, 2007

Java classpath issues (Problem 2)

The problem that I am going to address here was resolved by me by accident. When I used to try to run a java file using java filename , I used to get the error :
NoClassDefinitionFoundError filename

It was absolutely irritating. However after about 18 months of struggle(including re-installling my os several times in that period) I came up with a proper solution

This thing will most probably work provided that u hav the proper classpath set as I have mentioned in my previous post. Or else I hope that it should work anyway.

When running or compiling a file use the following-

javac -cp . Filename.java
java -cp . Filename


javac -classpath . Filename.java
java -classpath . Filename

What u actually are doing over here is that you are telling the java compiler to search for files in the current directory also (the “.” stands for the current directory) when it tries to compile or or run a java program.

Java classpath issues (Problem 1)

Ah well, in this blog I would like to address an issue that I had confronted me about 2 years ago. I really had to pull out all my hairs to resolve this problem a that time since I received no external help. But as good as your luck turns out to  be, I am just glad to be able to help. Setting classpath variables is a headache for novice java programmers. So ill try to make it as simple as possible.

To execute a java program from any directory in your system, u need to do the following(this
is for Windows XP users):
1) Right click on My Computer->properties
2) Go to the Advanced tab
3) You shall see a small button named Environment variables. Thats the
one u need to click.
4) A dialog box will open up showing the various system variables.
5) U need to add a new environment variable in the user variable dialog box. Click on new.

Add the following:
variable name : PATH
Variable value : the complete path to the bin folder of your java
installation directory

For example, on my system, the setting is the follows:

variable name : PATH
Variable value : C:\Program Files\Java\jdk1.5.0_12\bin

There you go. Just go and have a good time playing around with all the java files that u can, and experience the ease with which you can compile the files without setting the classpath each time or painfully copying the files to the bin folder.