Cannot load driver class: com.mysql.cj.jdbc.Driver - mysql

My Spring Boot Project trying connect to MYSQL database with driver mysql-connector-java.
I have import newest mysql driver and spring-boot-starter-data-jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I have configured database connection in application.properties file
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=somethingfunny
spring.datasource.password=somethingfunny
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
MYSQL version is 8.0.26
Spring boot version 2.6.2
When run project with Intellij I get error
Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory
method 'dataSource' threw exception; nested exception is
java.lang.IllegalStateException: Cannot load driver class:
com.mysql.cj.jdbc.Driver at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
~[spring-beans-5.3.14.jar:5.3.14] at
org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
~[spring-beans-5.3.14.jar:5.3.14] ... 35 common frames omitted Caused
by: java.lang.IllegalStateException: Cannot load driver class:
com.mysql.cj.jdbc.Driver at
org.springframework.util.Assert.state(Assert.java:97)
~[spring-core-5.3.14.jar:5.3.14] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:241)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:193)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:na] at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:na] at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
~[na:na] at
java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
~[spring-beans-5.3.14.jar:5.3.14] ... 36 common frames omitted
I have refered the post about Cannot load driver class: com.mysql.jdbc.Driver (NOT com.mysql.cj.jdbc.Driver), I cannot apply for my project because my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver.
I also refered this post Cannot load driver class: com.mysql.cj.jdbc.Driver. But i can not find correct answer(the answer is marked corrected) for this error.
How to fix this error ?

1) Use this dependency in pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
2) Use this property in your application.properties file
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

I have fix this issue. In Intellij right click to project, choose maven, choose reload project. Now Intellij will add mysql driver to the project

Going off of one of the above answers https://stackoverflow.com/a/70514438/14111809, but with a Gradle instead of Maven (in IntelliJ IDE), this fixed the issue for me:

Have you checked which version of MySQL connector do you have?
Since you haven't specified the version in your pom.xml, there is a chance that it pulled version 5 and now it is complaining about that .cj which is required for version 8.
That might be the reason why is it working without .cj ( com.mysql.jdbc.Driver ), because it pulled the version 5.
Manually add the version in your pom.xml and keep .cj as it is.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
After that make sure that you pull your dependencies.
But make sure that you check in your project if you do have ONLY version 8 after "mvn clean install".
As of the picture that you shared "mysql version", that means nothing, the exception is regarding mysql-connector jar, it has nothing to do with the workbench, you can still have workbench version 5, and use mysql-connector jar version 8, it will make no difference.
In any version of mysql workbench ( 5 or 8 ):
mysql-connector 8 jar = requires .cj
mysql-connector 5 jar = does not require .cj
The only thing that you need to do is define the version of mysql-connector-java in your pom.xml

Fix the line that sets jdbc driver class as below: I don't think it matters but driver-class-name is more common. And check if your build file is up to date. Might as well try re-building using pom.xml
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Related

Maven error: Could not transfer artifact: ArtifactTransferException

I user Spring Tool Suite 4 to build a Spring boot project. When I try to specify dependency MySQL JDBC driver, I got this error:
Could not transfer artifact com.mysql:mysql-connector-j:jar:8.0.31 from/to central (https://repo.maven.apache.org/maven2): Connect timed out
org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact com.mysql:mysql-connector-j:jar:8.0.31 from/to central (https://repo.maven.apache.org/maven2): Connect timed out
The first I use these code:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
And after that I found this guide https://blogs.oracle.com/mysql/post/mysql-connectorj-has-new-maven-coordinates so I change to
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
but it still does not work.
Please help me! It took me a lot of time.

Exception org.jboss.modules.ModuleNotFoundException: org.lg.log4j2 when attempting to add log4j 2 as module in jBoss eap 7

I am using log4j 2.16 for my EJBs. Im meaning to add the log4j-core (and log4j-api if necessarily ) as module in jBoss eap 7 but I keep getting the ModuleNotFoundException exception.
here is my configurations :
src/main/ressource/META-INF/jboss-deployement-structure.xml
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.lg.log4j2"/>
</dependencies>
<exclude-subsystems>
<subsystem name="logging"/>
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.16.0</version>
<scope>provided</scope>
</dependency>
jboss-eap-7.0 - 22\modules\system\layers\base\org\lg\main
<module xmlns="urn:jboss:module:1.1" name="org.lg.log4j2">
<resources>
<resource-root path="log4j-api.jar"/>
<resource-root path="log4j-core.jar"/>
</resources>
</module>
the two jar exist in the current folder.
EJBXX.jar.failed
WFLYSRV0179: Failed to load module: deployment.EJBXX.jar:main
Caused by: org.jboss.modules.ModuleNotFoundException: org.lg.log4j2:main\"}}"
I am not sure about my solution so please correct me if there is another way to achieve it.
N.B: I spot this error in my jBoss console :
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Your module is in the wrong directory. It needs to be in org/lg/log4j2/main. You’re also likely missing some module dependencies too.

Getting Started With SpringBoot, Spring Web, MyBatis & MySQL

I'm about to rewrite a Struts 2 webapp with SpringBoot & Spring Web.
How can I get started? I'm using start.spring.io with three dependencies: Spring Web, MyBatis Framework and MySQL Driver
I've opened the project in IntelliJ, built the project (no problem) but can't run the webapp as it fails to connect to my MySQL instance. Sure I realise that I'll need to provide DB URL, username, password, port, etc. but there's no configuration files included in the zip. Where do I add these so I can properly set the properties?
How can I get started with an up-to-date version of Spring Boot that includes the basic dependencies I need?
It's so frustrating to be hamstrung at the first hurdle when I know I'll be able to run the rest of the way once I get over this first step!
Thank you.
You can find a file named "application.yml" or "application.properties" in directory below.
src/main/resources/
So, you have to go into that, and put the configs there.
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true
Also, check to make sure you have the required dependencies added. You'll probably need these dependencies Spring Web, Spring Data JPA, and MySQL Driver.
For example, in case you're using Maven. You can go check the dependencies section of POM.XML.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
I highly recommend you to check this link.

Spring mysql-connector version incompatibility

i am coding a maven project with spring,
the default mysql-connector is version 6.0.5, and whenever i run the app on server the stack trace tells me that "Could not load JDBC driver class [com.mysql.jdbc.Driver]".
So i add as external library mysql-connector downloaded from mvnrepository.com version 5.1.40, add the dependency code to pom.xml and then it works!
How can i fix it using v 6.0.5?
thanks
If I understand you correctly you downloaded manually mysql connector, and added it as library.
Add dependency in pom.xml and reimport maven dependencies.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
You should checkout if your application.properties are set correctly, e.g:
spring.datasource.url=jdbc:mysql://localhost/jpa_example
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

MiniDFSCluster gives ioexception

I am trying to test in hadoop. have the code as:
System.setProperty("test.build.data","/folder");
config = new Configuration();
cluster = new MiniDFSCluster(config,1,true,null);
but in new MiniDFSCluster(config,1,true,null), it throws exception:
java.io.IOException: Cannot run program "du": CreateProcess error=2, The system cannot find the file specified.
at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
at org.apache.hadoop.util.Shell.runCommand(Shell.java:149)
at org.apache.hadoop.util.Shell.run(Shell.java:134)
at org.apache.hadoop.fs.DU.<init>(DU.java:53)
at org.apache.hadoop.fs.DU.<init>(DU.java:63)
at org.apache.hadoop.hdfs.server.datanode.FSDataset$FSVolume.<init>(FSDataset.java:333)
at org.apache.hadoop.hdfs.server.datanode.FSDataset.<init>(FSDataset.java:689)
at org.apache.hadoop.hdfs.server.datanode.DataNode.startDataNode(DataNode.java:302)
at org.apache.hadoop.hdfs.server.datanode.DataNode.<init>(DataNode.java:216)
at org.apache.hadoop.hdfs.server.datanode.DataNode.makeInstance(DataNode.java:1283)
at org.apache.hadoop.hdfs.server.datanode.DataNode.instantiateDataNode(DataNode.java:1238)
at org.apache.hadoop.hdfs.MiniDFSCluster.startDataNodes(MiniDFSCluster.java:417)
at org.apache.hadoop.hdfs.MiniDFSCluster.<init>(MiniDFSCluster.java:280)
at org.apache.hadoop.hdfs.MiniDFSCluster.<init>(MiniDFSCluster.java:124)
at ebay.Crawler.TestAll.testinit(TestAll.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:599)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified.
at java.lang.ProcessImpl.<init>(ProcessImpl.java:92)
at java.lang.ProcessImpl.start(ProcessImpl.java:41)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:463)
... 33 more
Could someone please give me some hint how to solve this?
Thank you very much.
Looks like the du command is not there on the system or is not in the PATH. If using Hadoop on Windows then Cygwin has to be installed. Anyway, which du will give the location of du binary.
I suspect you're using a Cloudera distribution of Hadoop. Version 1.0.0 of 'vanilla' Hadoop does work on Windows - at least create and writing to a file does.
If you need to run unit tests in a local Windows environment, try using Maven profile properties to set a version of 1.0.0 in you local Maven config, and in the POM specify the 'remote' config. The global setting will override the POM-specific one.
settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>windows</id>
<properties>
<hadoop.version>1.0.0</hadoop.version>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>windows</activeProfile>
</activeProfiles>
</settings>
pom.xml
<properties>
<hadoop.version>0.20.2-cdh3u2</hadoop.version>
</properties>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>${hadoop.version}</version>
</dependency>