0

I have a problem with the jdbc classpath in Java. I am using Maven, but jdbc is not handable within maven, so I created a "lib" folder with the mysql-connector.jar und added this jar to the build path of the project.

If I run it local in eclipse it is all working fine, but when I put the project on my server and try to run it via command line it prints out this error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/eventlist
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at WriteData.writeEventDataToDatabase(WriteData.java:24)
at Main.main(Main.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)

I am using Java 1.8.0_60 and Maven 3.0.5 on CentOS 6. I hope you can tell me how the add the jdbc file to the classpath.

thanks!

11
  • Of course Maven can handle JDBC drivers. You just have to register the JAR into the repository. You need the mvn:install command for any JARs that aren't in a central repository. Commented Sep 11, 2015 at 17:14
  • how is this working exactly ? Tried to use mvn:install and mvn install and then path to the jar, but it did not work Commented Sep 11, 2015 at 17:31
  • What does "did not work" look like? This command installs the JAR to your repository (might be your local .m2). Then you have to add it to your pom.xml. Commented Sep 11, 2015 at 17:34
  • -bash: mvn:install: command not found Commented Sep 11, 2015 at 17:40
  • Maven is not installed, then. Commented Sep 11, 2015 at 17:41

2 Answers 2

4

Driver should be in server too. For example, if you are using tomcat on your server then driver must be in lib folder of tomcat.

Sign up to request clarification or add additional context in comments.

2 Comments

i don't use a application server. I just want to run the project from command line
But it's a fair point for others: Tomcat 7.x and higher will expect to see JDBC driver JARs in a server /lib folder, not your application WEB-INF/lib. Same for other Java EE app servers, like JBOSS.
2

You need to do two things:

  1. Execute mvn:install install-file to get the JAR into a repository.
  2. Modify your project pom.xml to ask the repository for that JAR.

Here's an example of the mvn:install command:

call mvn install:install-file -Dfile=sqljdbc.jar -DgroupId=microsoft -DartifactId=sqljdbc -Dversion=4 -Dpackaging=jar

Here's the pom.xml entry that calls for it in my project:

<dependency>
    <groupId>microsoft</groupId>
    <artifactId>sqljdbc</artifactId>
    <version>4</version>
</dependency>

You'll do the same thing for every JAR your project needs that is not available in a public repository.

If I execute mvn:install on my local machine, I see the JAR installed in my local .m2 repository. I don't have access to a private, shared repository. If I want my local machine and an integrated build facility to both have access to that JAR I'll have to get it into a private, shared repository that both can see.

4 Comments

yes, the JAR got installed in my local .m2 repository, if i wanna run it thought, it prints out this error now: [ERROR] Failed to execute goal on project EventlistServer: Could not resolve dependencies for project at.flobau:EventlistServer:jar:0.1: Failure to find jdbcdriver:jdbc:jar:2.1 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1] I added a Dependency for this JAR in my pom.xml, so it is there.
Are you using Maven to package and execute? Sounds like you got the JAR into the repository, but not into the executable package. The error says it's looking in Apache Maven repo, not your local .m2.
i use mvn exec:java -Dexec to execute the program
Don't know. I use Maven with JBOSS or Spring Boot. Not having these issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.