java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable
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
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
You have to put the full path to the jarfile in the classpath (including the filename):
.;C:\j2sdk1.4.2_16\jre\lib;
C:\Program Files\mysql-connector-java-3.1.144\mysql-connector-java-3.1.14-bin.jar
As Hippo said, you have to restart cmd
after changing that. If it doesn't work, launch your program like this:
java -cp ".;C:\j2sdk1.4.2_16\jre\lib;
C:\Program Files\mysql-connector-java-3.1.144\mysql-connector-java-3.1.14-bin.jar"
my.class.Name
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Vertrigo server eclipse
You need to have mysql connector jar in your classpath while running the program. If you are running it through the command line then you can do something like this:
java -cp <.;path to sql jar> ShippingCompany
Why am I getting a NoClassDefFoundError in Java?
This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Classpath contains only the running file
Try don't mix -cp and -jar options, this might work:
java -cp "mysql-connector-java-5.1.26-bin.jar:MyJarFile.jar" my.package.Main
for *nix or
java -cp "mysql-connector-java-5.1.26-bin.jar;MyJarFile.jar" my.package.Main
for windows
where my.package.Main
is your main class.
Related Topics
Converting a String to an Integer on Android
How to Use Interface to Communicate Between Two Activities
Java Program That Runs Commands with Linux Terminal
Compile Code Using Javafx 2.0 (Using Command Line)
Reusing a Preparedstatement Multiple Times
Java Method to Swap Primitives
Safely Casting Long to Int in Java
Why Is the Catch(Exception) Almost Always a Bad Idea
Jlabel Mouse Events for Drag and Drop
How to Change the Classpath Within Java
How to Get a Context in a Recycler View Adapter
Can the Android Sdk Work with Jdk 1.7
How to Achieve Javafx Mouse Event "Push and Hold"
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Openqa/Selenium/Webdriver
Get Domain Name from Given Url
How to Execute a Java .Class from the Command Line
Hibernate Criteria Returns Children Multiple Times with Fetchtype.Eager