This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 4 years ago.
This question might have asked here number of times . After doing some google search for the above error and doing some update, I can't understand why I'm still getting that error. I've already put my driver-- mysql-connector-java-5.1.5-bin in the classpath:
Java_Home\jre\lib\
Java_Home\jre\lib\ext\
Java_Home\lib
and the code which I'm using to connect to mysql database is:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select message_body from deadletter");
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
}
con.close();
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
}
and the complete stacktrace of the above exception is:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at mail.main(mail.java:114)
Now, what's wrong I'm doing here?
If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:
Right click your project folder and open up Properties.
From the right panel, select Java Build Path then go to Libraries tab.
Select Add External JARs to import the mysql driver.
From the right panel, select Deployment Assembly.
Select Add..., then select Java Build Path Entries and click Next.
You should see the sql driver on the list. Select it and click first.
And that's it! Try to run it again! Cheers!
I too struggled with the same problem and finally got the solution for it.
Just copy the MySql-Connector.jar into Tomcat's lib folder, and then remove the jar from the webapp's lib folder, and then, run the project.
The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver
I have the same problem
but I found this after a long search:
http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-Load-Class.html
But I made some change. I put the driver in the same folder as my ConTest.java file,
and compile it, resulting in ConTest.class.
So in this folder have
ConTest.class
mysql-connector-java-5.1.14-bin.jar
and I write this
java -cp .;mysql-connector-java-5.1.14-bin.jar ConTest
This way if you not use any IDE just cmd in windows or shell in linux.
this ans is for eclipse user......
first u check the jdbc jar file is add in Ear libraries....
if yes...then
check...in web Content->web Inf folder->lib
and past here jdbc jar file in lib folder.....
1) Download connector from here https://www.mysql.com/products/connector/
2) Select JDBC driver for mysql
3) click on Platform Independent (Architecture Independent), ZIP Archive
4) Download the file and unzip it
5) (For Eclipse)Click Project->properties-> Java Build Path->Libraries
(For Netbeans) right click libraries on left bar-> add jar
6) Click on add external jar
7) select mysql-connector-java-5.1.40-bin.jar
8) Done!
If you are using NetBeans follow the following steps:
Right click on 'Libraries' and select 'Add Library..'
Scroll down the list of 'Global Libraries' and select 'MySQL JDBC Driver' and click on 'Add Library'.
What did you put exactly in lib, jre/lib or jre/lib/ext? Was it the jar mysql-connector-java-5.1.5-bin.jar or something else (like a directory)?
By the way, I wouldn't put it in lib, jre/lib or jre/lib/ext, there are other ways to add a jar to the classpath. You can do that by adding it explicitly the CLASSPATH environment variable. Or you can use the -cp option of java. But this is another story.
In NetBeans,
right click on libraries, select properties
select rub tab
there you need to add the mysql-connector-java.jar and apply
You should download MariaDB connector.
Then:
Expand your project.
Right-click on Libraries.
Select Add Jar/Folder.
Add mariadb-java-client-2.0.2.jar which you just downloaded.
Ok, i find solution changing the path of mysql-conector-java.jar to the follow path:
ProjectName/WebContent/Web-inf/lib/mysql-conector-java.jar
So you need to add the conector to project again and delete the previous one.
Most of the possible solution has been covered above. From my experience of this issue, i have placed the mysql-connector-jar in the /WEB-INF/lib folder of the webapp module and it worked fine for me.
In netbean
Right click on your project, then you click on Libraries, then Run tab, add library, then select mysql connector
In IntelliJ
Do as they say in eclipse
"If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:
Right click your project folder and open up Properties.
From the right panel, select Java Build Path then go to Libraries tab.
Select Add External JARs to import the mysql driver.
From the right panel, select Deployment Assembly.
Select Add..., then select Java Build Path Entries and click Next.
You should see the sql driver on the list. Select it and click first.
And that's it! Try to run it again! Cheers!"
Here we have to add the jar file in Project Structure -> Libraries -> +(add)
I keep the mysql-connector jar with my project rather than in Javahome. As a result, you can be sure it can be found by being sure its in the local classpath. A big upside is that you you can more the project to another machine and not have to worry about (or forget) to set this up again. I personally like including it in version control.
Ok..May be i can also contribute my solution..
Right click on project -properties -->Deployment assembly...there you need to add the mysql-connector-java.jar and apply it...which makes your prject configured with web-Libraries that has this sql connector...This worked for me..
I hope this works for you guys as well
The only thing that worked for me was downloading the mysql-connector-java-5.0.8-bin.jar directly from the MySQL website:
http://dev.mysql.com/downloads/connector/j/3.1.html
Then if you're using Eclipse paste this mysql-connector-java-5.0.8-bin.jar in the WEB-INF lib folder
If doesn't works try with any of the solutions posted in this link:
http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html
I experienced the same error after upgrading my java to 1.8.0_101. Try this:
i. Remove the mysql.jar fro your buildpath.
ii. Clean the server and project
iii. Add the jar file back to the build path.
Worked for me.
Just copy the MySQL JDBC drive jar file and paste it to Tomcat or whatever is the server's lib folder. It works for me.
just copy and paste the mysqlconnector jar to your project folder and then build path it will definitely work.
see to it that the argument of Class. forName method is exactly "com. mysql. jdbc. Driver".
If yes then see to it that mysql connector is present in lib folder of the project. if yes then see to it that the same mysql connector . jar file is in the ClassPath variable (system variable)..
I want to use the Light Weight Java Gaming Library(LWJGL) to my Netbeans so I can use it in my Java application. The only videos that I can find show the zip file that they downloaded with separate src and doc folders inside of it. The zip file that I download has everything in one directory. I went to lwjgl.org/download and clicked on Stable and then Generate Bundle. What am I missing?
I had the same problem recently.
So, to begin you want to go to Tools in the context menu and select Libraries (as shown). Next you can add a new library and name it e.g. LWJGL-3.1.1 confirm with ok. You can find 3 tabs in the current window Classpath, Sources and Javadoc. There you add your jar files accordingly (in the downloaded .zip file you find .jar files with different names like lwjgl-{whatever}-sources.jar or lwjgl-{whatever}-javadocs.jar) make sure you put them in the right place. You have to repeat this process for all of the jar files you want. The javadoc files are not required but recommended. Make sure you also collect all the native .dll files and merge them in a folder called \natives. You find them in these jar files that are called like: lwjgl-{whatever}-{your-OS}.jar.
Once you have finished the setup for your library right click on your current project and choose Add Library.... In the window that pops up you scroll down until you find your library that you have just created and you are almost done now.
Last but not least go to the project settings. Select Run and make sure that you set the classpath in VM options to something like in the image: -Djava.library.path="C:\java_workspace\LWJGL Library 3.1\natives. Now this classpath tells netbeans where your native files are located. Your \natives folder that you should have created in the beginning is where this path should lead to. That's it. This is all you have to do for a setup without the use of maven, gradle, ...
You can test if it is working with the code provided by LWJGL HelloWorld example.
I hope this solved your problem.
Best regards.
I work in an organisation where i am provided with a folder having codes and other things for particular application.
I want to open this folder in eclipse and be able to navigate to all the classes that are used through resources(ctrl + click).
The problem is when I imported the folder the eclipse worked same as that of a notepad. Any changes no error would be shown.
I even added the required jar files for that project.
What should I do? I just need to navigate and check out the classes that are used and declared. Pls Help.
P.S. - I work on Ubuntu Machine.
If you have a java project, you may need to configure the source directory(s).
This can be done by right clicking on your folder -> Build Path -> Use as Source Folder.
How do i give the path for the .dll or .so file in netbeans to load the library using
System.loadLibrary("Foo")
I read this on netbeans page but couldn't help me. as it gives the unsatisfied link error.
These are the two things i tried :
In the following snapshot i created a lib folder inside the netbeans project and placed the dll files inside it.
In the second snapshot i created a lib folder inside the modules folder and placed all the dll files inside lib folder as stated in the link.
But both of them give me unsatisfied link error exception. How do i set java.library.path in netbeans so that i can directly test the application from netbeans,
without having to go and write on the terminal ?
Its working now. Some little bit setting you have to do with your Netbean IDE.Only of Netbean IDE.
Follow the Steps :-
==>Right click on the Project
==>Properties
==>Click on RUN
==>VM Options : -Djava.library.path="C:\Your Directory where Dll is present"
==>Ok
Its working 100%. I have done this in my own project.
I'm using Mac OS X Yosemite and Netbeans 8.02, I got the same error and the simple solution I have found is like above, this is useful when you need to include native library in the project. So do the next for Netbeans:
1.- Right click on the Project
2.- Properties
3.- Click on RUN
4.- VM Options: java -Djava.library.path="your_path"
5.- for example in my case: java -Djava.library.path=</Users/Lexynux/NetBeansProjects/NAO/libs>
6.- Ok
I hope it could be useful for someone.
The link where I found the solution is here:
java.library.path – What is it and how to use
To me JDeveloper's suggestion didn't work. However, the method that I came across this early afternoon works handy.
It is for netbeans 8.02, and Visual Studio 2013 generated dll.
create a package(eg, "dll") under [Source Packages] node of netbeans
project
put/copy a dll file(eg, simpleDLL.dll) into the package(eg,
"dll")
on the [Libraries] node of the netbeans project, right-click
and choose "Add Jar/Folder...",
navigate to the folder("src") that contains the folder("dll") for the package that you created in step 1 above (it could be "...\src\dll" in my example), select package folder("dll") and click [Open] button to select it.
-- now you should have added a folder("dll") (which contains the dll file{simpleDLL.dll}) to the Libraries node of the project.
That's it. It should work!
Good luck.
IF you are working on "NetBeans Modules"
DLLs or SOs can be placed in the folder release/modules/lib/ in a module project's sources (look in the Files tab). This will make them appear in the final NBM or application in a lib subdirectory beneath where the module's JAR resides. Then just use System.loadLibrary as usual.
I've got a Vaadin project that uses JIRA's SOAP API. It runs fine in Eclipse, but attempting to make it work in IntelliJ and with Maven is slowly driving me mad, though I feel I'm close to completion.
Whenever I deploy my application to a local Tomcat server, all I get is a stack trace caused by this:
java.lang.NoClassDefFoundError
be.azvub.jira.service.JiraSoapServiceServiceLocator.getJirasoapserviceV2(JiraSoapServiceServiceLocator.java:53)
This class exists, because nothing has changed since I moved them from eclipse and because I can open the .java file and see that there are no errors in there. I can remove the luine of code that makes the call to this class and all is well, so it's a localised problem. The problem class is in a different package from my Application class (which still works), where the call to it is made.
What's causing this problem and how can I fix it?
I had the same problem and fixed it now after debugging ...
IntelliJ usually auto-creates a folder /lib under your /src folder inside your project, and then it would tell your project that all LIBs are in there, on that basis; we usually assume that it's the place to put all your LIBs in there as well.
Although with that setup your project would compile fine since IntelliJ can link up to your JARs However, with that sort of setup Tomcat will fail to execute, since Tomcat expects to find the classes under /WEB-INF/lib,...
Therefore, the solution is to:
1) Drag your LIB folder (sorry I mean "/lib") from /src/lib to be
under /web/WEB-INF directory
2) You would get a warning about moving classes / JARs, say YES.
(You need to tell your project to re-map your existing pre-defined
LIBs to the new folder):
3) From the main menu, select FILE -> Project Structure
4) Select
Libraries from the left menu
5) If you don't see any existing libs, then you're done, click OK
6) If you do see libs in there, then:
7) Click on each LIB from the
middle-list, and then remove the ones that can't be located,
8) Re-add them again from the new location
9) Repeat (7) to all other LIBs.
10) OK,
RE-compile, your project should deploy on Tomcat now and work fine.
Regards
Heider
I don't know how I did it, but by fixing an unrelated problem, this got fixed along with it.
I changed the Web Resource Directory under File | Project Structure | Facets (So the app could find Vaadin's theme folder under the WebContent directory instead of the default src/main/webapp it was set to) Neither of the folders actually contain the class that was giving me problems.
I also forced Maven to reimport everything, as I have done several times before, but now I used the option to do so under the project's context menu in the Project Explorer instead of the Maven Projects tab, so maybe that made a difference? Maven is still largely voodoo to me, but at least everything seems to be working normally now.
But thank you for your assistance anyway.
My guess would be the build path, check to see if the class you are calling is present in the build path of the project.