Hi I am running a java program in command prompt. I have set the "C:\j2sdk1.4.2_09\bin" in PATH System variable.Then I am able to compile and run the program.But as I need to set the mysql-connector I have set this in "C:\mysql-connector-java-5.1.10\mysql-connector-java-5.1.5-bin.jar" CLASSPATH variable but now I am able to compile the program as I run the program I get "Exception in thread main java.lang.NoClassFoundDefError". How is this? Can anybody elaborate on this?
It's fairly unclear what you're asking here. Are you saying you changed your CLASSPATH and now the class can't be found?
One thing to keep in mind is that the CLASSPATH does not, by default, include the current directory. You have to add it (.
) if you want it in the CLASSPATH, e.g.:
set CLASSPATH=.;C:\mysql-connector-java-5.1.10\mysql-connector-java-5.1.5-bin.jar
(Note I put a .;
at the beginning of that.)
PATH and CLASSPATH are completely unrelated. PATH tells Windows where to find executable programs (and does implicitly include the current directory) when you run them without giving it an explicit path. CLASSPATH tells the Java runtime where it can load classes from.
Apologies if this is completely off-base; if so, could you clarify your question?
Agree with the answer above, because I've done this a million times myself. The exception you're getting is almost definitely related to your classpath not including the "." which tells Java to include the current directory in its death for classes.
It seems counter intuitive that Java wouldn't automatically include the current directory in its search, but anytime you manually override the classpath, you have to include this.
The reason? When you don't specify a classpath then a default one is used, which includes the ".", and which is transparent to you.