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

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.

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

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.

What worked with me using Netbeans was:
Run > Set Project Configuration > Customize.
Under Libraries > Add Library. Added MySQL JDBC Driver (I assume it appeared in list because I copied the jar file to the jre\lib\ext folder.
And it worked seamlessly.
I tried setting classpath but that did not work. I am using Netbeans 7.0

simply do a right click on your project in "Netbeans" select properties then click on "libraries " then click on "add library..." button then select "MySQL JDBC Driver" and click on "add library" button then on "OK" button

I also had this problem before, but after I put/added mysql-connector-java-5.1.34-bin.jar (Download it from here) into the apache-tomcat-8.0.15\lib folder, and then ran my project, it really did work.
Note : Even after adding the jar file the error persists, then restart the Tomcat server and rerun you project again.

Open Netbeans IDE
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under "Compile" tab - click Add Jar/Folder button.
Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com)
Click OK
Run Again... Its work.

If you are using maven, add the dependency to pom.xml should solve the problem.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>

In Netbeans IDE just Check the properties of Project on which you working on,in properties window go to 'library' tag, in diolog box just add your mysql-connector-java-**.jar file.

I had this same problem in Netbeans. Because I was using a tomcat connection pool as defined in context.xml I needed to add the jdbc jar to both the project (Properties->Libraries) and to the lib/ folder within my Tomcat server so it could be seen on startup.

Related

Postgresql/Eclipse ERROR No suitable Driver found for jbdc [duplicate]

Ok I am on Win Vista and correctly set my MAVEN_HOME, JAVA_HOME stuff. but I do not have a class path yet. I have also installed MySQL. now I have opened a Hibernate book and at the very first pages it says "make sure the jdbc driver is in your classpath" . I have also downloaded some Zip file that is ConnectorJ or some name like that which is basically the mySql driver for java... but my problem for now is this sentence that I have no clue how to do it: "make sure the jdbc driver is in your classpath"
would you please help me about this classpath thing?
thanks
Here is a good tutorial regarding, setting the class path. Further you might like to read Managing the Java classpath (Windows).
Having said that, you should not set the classpath for your driver in Windows environment variable. Instead, you should include that driver jar inside your IDE under project properties. But I noticed that you are actually using Maven. In this case you should look for the driver under Maven in order to fulfil that dependency. Maven will download the driver jar, if doesn't exist, and make it local.
In case, you are not using any IDE, then you can create a lib directory and tell the compiler that all required jars are in there, at the time of compilation/execution. You can find HOW, in the former link given above.
Since you are using Maven, you just need to put the JDBC driver as a dependency in your pom.xml file. Maven will add it to the classpath whenever it compiles/runs your application.
What you do when deploying your application is dependent on the technologies in use.
If it's a command line application create a %CLASSPATH% variable or add the path to the jdbc.jar file using the java -cp {path\to\jdbc.jar} option.
If it's a web application, you'll need to package the driver jar in your .war/.ear/.sar (the maven assembly plugin can do this) or include it in the ./lib folder of the application container and declare it as scope=provided in maven.
Steps to setup JDBC for Eclipse projects
Download JDBC zip archive from
https://dev.mysql.com/downloads/connector/j/5.0.html
Extract the file and copy the executable jar file to program files->Java->jdk->bin
Right click on the project and select Buildpath->Add external archives->(Jar file)
Compile the program
you can directly connect database by following steps:
1) download mysql-connector-java 5.0.8 and extract the files.
2) then place the folder in program files.
3) then simply add this library on your project by right clicking on it.
4) and here you go. Run your app with db connectivity.

Ant build with Eclipse Mars fails due to missing classpath reference

I recently moved to Eclipse Mars and installed JDK 8u51. I'm using the JBoss Tools plugin with my Eclipse and have a couple of Ant files to build my projects.
When I double-click on a build file in the Ant view, however, my project won't built. Instead I get this error message:
The archive: C:/Program%20Files%20(x86)/Eclipse%20Mars/plugins/org.eclipse.swt.win32.win32.x86_64_3.104.0.v20150528-0211.jar which is referenced by the classpath, does not exist.
I checked the file system and the JAR is actually there. Any ideas how I could fix that? Maybe it's the spaces (%20) in the path name?
Unfortunately, I don't know where this dependency comes from. In the External Tools Configuration it is under Additional Tasks & Support, but I cannot edit the entry there. Nor did I find any other dialog where I can actually configure this reference.
Having had the exact same issue with Eclipse Mars, it turned out the problem was related to the '%20' stuff and not to the jar in itself.
You can either work on a different folder than "Program Files" (install eclipse somewhere else, ...), or create a symbolic link so that ant can see the existing repository the way it knows it. Run the following command in the elevated command prompt(Start > Type cmd > Right click and Run as administrator)
mklink /d c:\Program%20Files%20(x86) "c:\Program Files (x86)"
(and the same for "Eclipse Mars" directory, that you could also rename to remove the space)
Only need to remove blank spaces in the directory name.
Eclipse 4.5.1 was released on October the 2nd and Bug 470390 has been fixed.
The update resolves this issue.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [duplicate]

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.
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
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.
What worked with me using Netbeans was:
Run > Set Project Configuration > Customize.
Under Libraries > Add Library. Added MySQL JDBC Driver (I assume it appeared in list because I copied the jar file to the jre\lib\ext folder.
And it worked seamlessly.
I tried setting classpath but that did not work. I am using Netbeans 7.0
simply do a right click on your project in "Netbeans" select properties then click on "libraries " then click on "add library..." button then select "MySQL JDBC Driver" and click on "add library" button then on "OK" button
I also had this problem before, but after I put/added mysql-connector-java-5.1.34-bin.jar (Download it from here) into the apache-tomcat-8.0.15\lib folder, and then ran my project, it really did work.
Note : Even after adding the jar file the error persists, then restart the Tomcat server and rerun you project again.
Open Netbeans IDE
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under "Compile" tab - click Add Jar/Folder button.
Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com)
Click OK
Run Again... Its work.
If you are using maven, add the dependency to pom.xml should solve the problem.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
In Netbeans IDE just Check the properties of Project on which you working on,in properties window go to 'library' tag, in diolog box just add your mysql-connector-java-**.jar file.
I had this same problem in Netbeans. Because I was using a tomcat connection pool as defined in context.xml I needed to add the jdbc jar to both the project (Properties->Libraries) and to the lib/ folder within my Tomcat server so it could be seen on startup.

`java.lang.ClassNotFoundException` when run it in cmd [duplicate]

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.
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
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.
What worked with me using Netbeans was:
Run > Set Project Configuration > Customize.
Under Libraries > Add Library. Added MySQL JDBC Driver (I assume it appeared in list because I copied the jar file to the jre\lib\ext folder.
And it worked seamlessly.
I tried setting classpath but that did not work. I am using Netbeans 7.0
simply do a right click on your project in "Netbeans" select properties then click on "libraries " then click on "add library..." button then select "MySQL JDBC Driver" and click on "add library" button then on "OK" button
I also had this problem before, but after I put/added mysql-connector-java-5.1.34-bin.jar (Download it from here) into the apache-tomcat-8.0.15\lib folder, and then ran my project, it really did work.
Note : Even after adding the jar file the error persists, then restart the Tomcat server and rerun you project again.
Open Netbeans IDE
Right-click your Project.
Select Properties.
On the left-hand side click Libraries.
Under "Compile" tab - click Add Jar/Folder button.
Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com)
Click OK
Run Again... Its work.
If you are using maven, add the dependency to pom.xml should solve the problem.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
In Netbeans IDE just Check the properties of Project on which you working on,in properties window go to 'library' tag, in diolog box just add your mysql-connector-java-**.jar file.
I had this same problem in Netbeans. Because I was using a tomcat connection pool as defined in context.xml I needed to add the jdbc jar to both the project (Properties->Libraries) and to the lib/ folder within my Tomcat server so it could be seen on startup.

Java classpath - one jar accessible, other not. Why?

I'm trying to include a weka-src.jar file in Netbeans project and use it.
I already have set up my CLASSPATH and included :
C:\Program Files\Java\jre6\lib\ext\weka-src.jar into it
My JAVA_HOME variable is set to:
C:\Program Files\Java\jdk1.6.0_25\bin
but still I get the error in Netbeans: package doesn't exist and NetBeans doesn't auto-complete when I'm trying to add it to imports
Similar to this I jave a jar file
C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.0.8-bin.jar
also included in CLASSPATH and after adding to project Netbeans does the auto-complete thing.
My cmd:
echo %CLASSPATH%:
.;C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.0.8-bin.jar;C:\Program Files\Java\jre6\lib\ext\weka-src.jar
Tried in Eclipse, the import part is not underlined, but still I can't use the classes, as if weka-src.jar wasn't even added
I can't figure out what is wrong. Any hints, please?
Maybe there is someone who can test it in his environment? weka-src.jar is in Weka's program files folder.
I am guessing that weka-src.jar contains source code which need to be compiled before it can be used. Perhaps there is a weka.jar which has the compiled classes in it.
In netbeans, you don't make classpath with windows environments variables, but with netbeans.
In netbeans right clic on your project node in window projects, choose Properties, and in properties Librairies. Add your jar.project/librairies with that.
Istao is correct. For NB you need to add jars via Properties

Categories