I am writing my code in Intellij and created a Sqlite Database for some data. I connected it with the sqlite-jdbc-3.23.1.jar driver inside of intellij and can run it successfully there.
When I create a JAR (or multiple Jars because of the Modules) and start the programm, I get this error message:
java -jar ServerAbschalteHotline.jar java.sql.SQLException: No
suitable driver found for jdbc:sqlite:src/Resources/credentials.db at
java.sql.DriverManager.getConnection(DriverManager.java:689) at
java.sql.DriverManager.getConnection(DriverManager.java:270) at
com.company.SQLiteHandler.connect(SQLiteHandler.java:20) at
com.company.SQLiteHandler.getCredentials(SQLiteHandler.java:30) at
com.company.HttpsServer.makeServer(HttpsServer.java:64) at
com.company.Main.main(Main.java:21)
So for me it looks like the programm couldnt find the driver after it get exported. I checked that the .jar file is there (with all the other .jar files) and tried to include it as a libary too.
I guess I missed one place where I have to add the jar, so where is it?
In the end a clean jar (downloaded the same version of the jar from the same page) and a recompiling solved the problem. I dont know where the real problem was, maybe the jar contains error(s)
I don't know if this will help, but when creating the jar I make sure that the JDBC driver is included.
There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.
I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:
import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
Connection connection = null;
PreparedStatement query = null;
try {
out.println("Create the driver instance.<br>");
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Get the connection.<br>");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
query = connection.prepareStatement( "SELECT * FROM customers");
//...
} catch (Exception e)
{
out.println(e.toString()+"<br>");
}
}
//...
When I run the above code I get the following output:
Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:
Download mysql-connector.
Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
Opened the project properties in Eclipse.
Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.
Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?
As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.
An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.
See also:
How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
How should I connect to JDBC database / datasource in a servlet based application?
Where do I have to place the JDBC driver for Tomcat's connection pool?
JDBC CLASSPATH Not Working
Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application war or put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.
The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.
Rather than an empty main(), try something like this, adapted from the included documentation:
public class LoadDriver {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
}
}
On my platform, I'd do this:
$ ls mysql-connector-java-5.1.12-bin.jar
mysql-connector-java-5.1.12-bin.jar
$ javac LoadDriver.java
$ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver
On your platform, you need to use ; as the path separator, as discussed here and here.
Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.
What you should not do do (especially when working on a shared project)
Ok, after had the same issue and after reading some answers here and other places. it seems that putting external lib into WEB-INF/lib is not that good idea as it pollute webapp/JRE libs with server-specific libraries - for more information check this answer"
Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.
Good solution 1
Create vendor folder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to
add your folder to the build path
Project Properties -> Java build path
Libraries -> add external lib or any other solution to add your files/folder
add your build path to deployment Assembly (reference)
Project Properties -> Deployment Assembly
Add -> Java Build Path Entries
You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
Select the ones you want and hit Finish.
Good solution 2
Use maven (or any alternative) to manage project dependency
Just follow these steps:
1) Install eclipse
2) Import Apache to eclipse
3) Install mysql
4) Download mysqlconnector/J
5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/
then give the same path as the address while defining the driver in eclipse.
That's all very easy guys.
If the problem still persists,
Put the-
mysql-connector-java-5.0.8-bin jar in a place inside your Tomcat->lib->folder (No matter where you've installed your Tomcat). And change your environmental variable (Done by clicking Properties of Mycomputer -Advanced system settings- Environmental variables-And set a new variable name & variable values as the place where your lib file resides.Dont forget to enter a ; at the end of the path)
If still problem persists
Try downloading commons-collections-2.0.jar (http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) and paste the jar in the same place where your mysql jar resides (ie) inside Tomcat-lib.
Clean your project-Stop your server- Finally try to run.
Many times I have been facing this problem, I have experienced ClassNotFoundException.
if jar is not at physical location.
So make sure .jar file(mysql connector) in the physical location of WEB-INF lib folder. and
make sure restarting Tomcat by using shutdown command in cmd.
it should work.
The only solution worked for me is putting the .jar file under WEB-INF/lib . Hope this will help.
assuming your project is maven based, add it to your POM:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.
Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute
My issue was a little different. Instead of jdbc:oracle:thin:#server:port/service i had it as server:port/service.
Missing was jdbc:oracle:thin:# in url attribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's
java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'
for this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
you need to:
Import java.sql.*;
Import com.mysql.jdbc.Driver;
even if its not used till app running.
I have created a Java application that uses a database, and I now want to build it into a jar file and make it so that the database works without NetBeans. I thought NetBeans would automatically include the database, but it doesn't.
The files that NetBeans include in the 'dist' folder when I build it look like this:
dist
lib
derby.jar (Executable Jar File)
derbyclient.jar (Executable Jar File)
README
WordFinder.jar (Executable Jar File)
Everything except the database-related code works when I launch the WordFinder.jar file after closing NetBeans.
I noticed that the database is using 'org.apache.derby.jdbc.ClientDriver', and I'm wondering, does that need to be 'org.apache.derby.jdbc.EmbeddedDriver' in order for it to include the database?
Are there any files that I need to include? I'm new to Java databases, so try to keep the instructions simple.
You need to start the database server in you computer, for doing that you need to go to your derby path (C:\Program Files\Java\jdk1.8.0_45\db\bin) and run the startNetworkServer.bat file
I may be a year late but seeing the comments, I'm assuming the issue hasn't been resolved yet. I had the same problem and would like to show how I worked around it. Note I'm not using an embedded derby database.
1) I changed my connection string. Instead of the usual "jdbc:derby://localhost:1527/myDB;create=true;...." , I put the exact location of the database which can be found usually under C:\Users\myPC\.netbeans-derby\myDB
2) Clean and build the project which should give you the "dist" folder which would include the jar file of the project together with the "lib" folder.
3) Start the StartNetworkServer.bat which is located in the bin folder under derby folder. A cmd would show that will say something "Apache Derby Network Server blah blah blah started and ready to accept connections on port 1527"
4) After doing those, run the jar file and it should run even with the netbeans IDE closed.
That's about it. That's how I got mine to work without opening Netbeans IDE. Hope it helps.
I made a Java application using netbeans and used sqlite as database
Everything works in netbeans but when i export the program to jar file and run it on another machine, an exception appeared when i try to connect sqlite database
The exception is about this line: Class.forName("org.sqlite.JDBC");
and the exception is: java.lang.ClassNotFoundException: org.sqlite.JDBC
I looked at all related threads in stackoverflow and no pure answer about this.
Thanks in advanced,
Just add the sqlite driver jar to your class path.
You application can't find the class org.sqlite.JDBC so it means a jar is missing in your class path.
Either include the sqlite jdbc driver in the jar file or make sure that you have it in your classpath. Add it as -classpath sqlite driver
Problem Solved
I had import the sqlite jar file for both compile time and run time. From netbeans -> project properties -> Libraries.
and Then i moved the exported jar file from dist folder in addition to lib folder to another machine .. and worked smoothly .. Thanks Guys up
I'm trying to build a project in clojure that takes data from a
Microsoft SQL Server database, and pushes it to a MySQL database.
The project runs fine using lein run, but when I package it into an
uberjar using leiningen, and run it using java -jar, it fails with:
Exception in thread "main" java.sql.SQLException:
No suitable driver found for jdbc:sqlserver....
This error only happens when I attempt to use both the MySQL database and
the SQL Server database. If I use any one of them on its own, the jar file
runs fine using java -jar.
My project.clj is as follows:
(defproject sqlserver-clojure "1.0.0-SNAPSHOT"
:description "A minimal example of the MySQL/SQLServer conflict"
:dependencies [[org.clojure/clojure "1.4.0"]
[com.microsoft/sqljdbc4 "3.0"]
[clojureql "1.0.4"]
[mysql/mysql-connector-java "5.1.6"]]
:main sqlserverclojure.core)
The problem is most likely that when making the uber-jar, you are not copying some files (eg .properties or XML config) that are required by the driver, or you are overwriting files that exist in both JDBC drivers.
A good example of overwriting is the META-INF/services/java.sql.Driver file which all JDBC 4 drivers have. This file contains a list of all classes in the jar implementing java.sql.Driver, so that the java.sqlDriverManager class can automatically load all Driver implementations using ServiceLoader.
If you create an uber-jar with multiple JDBC drivers, you either need to make sure this file contains the union of all these files, or your application needs to explicitly load the drivers you need with Class.forName("<name of the java.sql.Driver implementation>") for each required driver and not depend on the JDBC 4 driver autoloading.
Also verify that the process you use to create the uber-jar copies all resources and not just .class files!
Better yet (in my opinion), don't use an uber-jar, but keep the dependent .jar files external and reference them in the Class-Path entry of the META-INF/MANIFEST.MF file of your application, this saves you a lot of hassle to get your uber-jar working correctly (or verify that it is actually working correctly).
Adding this for my own reference since I've not touched Java before and struggle with Java interop.
Add
(. Class (forName "com.microsoft.sqlserver.jdbc.SQLServerDriver"))
In the namespace with your jdbc clojure function calls.