Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable

Tags:

I am trying to connect to mysql database using java on windows7. In spite of adding the complete url of jdbcdriver jar file in CLASSPATH, java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is thrown. Could anyone tell me what i am missing here? It works if I add the jar file in project library but I want to do it by CLASSPATH itself. My classpath looks like this- C:\jython2.5.1\javalib\mysql-connector-java-5.1.12-bin.jar

I want to make it clear that this is not the actual project i am working on. I am actually using Django with Jython, which requires the JDBC driver to access the database. That is the reason why I have to do it using CLASSPATH only.

like image 865
Nitin Garg Avatar asked Apr 07 '10 10:04

Nitin Garg


People also ask

What causes Java Lang ClassNotFoundException?

The java. lang. ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. The Java ClassNotFoundException is a checked exception and thus, must be declared in a method or constructor's throws clause.

How do I fix Java Lang ClassNotFoundException in eclipse?

click on project->properties->Java build path->Source and check each src folder is still valid exist or recently removed. Correct any missing path or incorrect path and rebuild and run the test. It will fix the problem.

Which method throws the ClassNotFoundException?

Class ClassNotFoundException. Thrown when an application tries to load in a class through its string name using: The forName method in class Class . The findSystemClass method in class ClassLoader .


2 Answers

The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp, -classpath, -jar arguments. It is ignored by IDEs like Eclipse, Netbeans and IDEA.

That environment variable is in real world also considered a poor practice since it breaks portability. I.e. program X will run successfully while program Y won't run without altering the CLASSPATH. It's only "useful" for Sun Oracle to prevent that starters get tired of typing the same classpath again and again in the -cp or -classpath arguments when following Java tutorials. In real world, batch/shell files are preferred where just the entire command with -cp/-classpath argument is specified.

In your case you're using an IDE. The classpath is there called the "Build Path". In plain Java projects, it represents both the compiletime and runtime classpath. You can configure it in the project's properties. You can add a complete folder, you can add individual/external JAR files, you can link projects, etcetera. Make use of it. Forget about using the CLASSPATH environment variable. It was a mistake by Sun Oracle. They thought to convince starters, but it ended up to be only more confusing to starters as they incorrectly interpret that environment variable as the classpath.

See also:

  • How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
like image 108
BalusC Avatar answered Oct 26 '22 15:10

BalusC


What finally helped me out was to copy the mysql-connector-java-5.1.15-bin.jar to \jre\lib and to \jre\lib\ext both(!) even though I did all the classpathing circus Java offers :) Environment was pure notepad/commandline though.

like image 24
ValGe Avatar answered Oct 26 '22 15:10

ValGe