Failed to determine a suitable driver class in CMD - java

Im developing spring boot Rest API, application,
In my VS code application is running without any runtime errors and working with postman too,
VS code runtime output
enter image description here
however, when i export the JAR file via VS code, and try to run cmd, that file pop up run time error,
ERROR
Blockquote
enter image description here
Blockquote
Error Msg in cmd
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
My POM XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
application.properties
## Server Properties
server.port= 8083
server.servlet.context-path= /TEST-API
spring.datasource.url= jdbc:mysql://localhost:3306/TEST?useSSL=false&useLegacyDatetimeCode=false&zerodatetimebehavior=Converttonull
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username= root
spring.datasource.password= 1234
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1
Could you please help me on this?

Option 1
Try use
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
instead of
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
in your properties file, if you are using mysql version < 8
Option 2
Check if your driver of mysql (JAR file) was included in the library of WAR --> WEB-INF/lib.
Option 3
If you are not fix it yet, and you have other database in your machine like a postgres:
change this connection to this another database.
test inside of your editor/ide
export to war
if the problem happens the same way, you can be right that your export process is the problem. So try use the plugin of maven to generate the war: https://maven.apache.org/plugins/maven-war-plugin/usage.html

Thanks for your support
one of my friends help me to fix the issue
what they did is,
delete mvnw file from the project path.
Then paste apache-maven-3.6.0 in to C driver and then
did the maven install and package the project in vs code.
After that project jar generated.

Related

Why is an error produced due to the MySQL driver and JDBC when I run my Spring Boot app?

When running the Spring Boot app, I have these error messages:
"Caused by: java.lang.RuntimeException: Driver
com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl,
mysql://localhost:3306"
and
"Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate
[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]:
Factory method 'entityManagerFactory' threw exception; nested
exception is java.lang.RuntimeException: Driver
com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl,
mysql://localhost:3306".
enter image description here
I have added the dependencies in the POM file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
Java version is:
<properties>
<java.version>17</java.version>
</properties>
Just from a quick look at it, it looks like your jdbc connector address isn't complete. You are missing a db there, something like
jdbc:mysql://localhost:3306/ReallyNiceDb
In MySQL, you will need to create a database, in this case, called ReallyNiceDb. Then, to this database, you will need to allow your user to interact with that database.
More info can be found here:
https://dev.mysql.com/doc/refman/8.0/en/creating-database.html

Error when trying to connect to Azure Eventhub: AzureTokenCredentials / IllegalArgumentException: name

I am following a tutorial on how to setup a connection to Azure EventHub in Spring Boot: [1]
When starting the spring boot app, I get the following error:
2021-01-07 13:37:25.447 WARN 16444 --- [ main] ConfigServletWebServerApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
'resourceManagerProvider' defined in class path resource
[com/microsoft/azure/spring/cloud/autoconfigure/context/AzureContextAutoConfiguration.class]: Unsatisfied
dependency expressed through method 'resourceManagerProvider' parameter 0; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'azure'
defined in class path resource
[com/microsoft/azure/spring/cloud/autoconfigure/context/AzureContextAutoConfiguration.class]: Unsatisfied
dependency expressed through method 'azure' parameter 0; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'credentials'
defined in class path resource
[com/microsoft/azure/spring/cloud/autoconfigure/context/AzureContextAutoConfiguration.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.microsoft.azure.credentials.AzureTokenCredentials]: Factory method 'credentials' threw exception;
nested exception is java.lang.IllegalArgumentException: name
my.azureauth looks like analogous to the on in [1].
I am using Java 11. Here is the relevant part of my pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-cloud-azure-eventhubs-stream-binder</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus-jms-spring-boot-starter</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Workaround. -->
<!-- Apparently com.microsoft.azure:spring-cloud-azure-eventhubs-stream-binder:1.2.7 has a transitive -->
<!-- dependency to the following package. However, it seems that the version is not pinned correctly, so -->
<!-- we have to pin the version to a compatible one as a workaround. -->
<!-- 7.5 is apparently the latest version in which com.nimbusds.oauth2.sdk.http.CommonContentTypes is available. -->
<!-- For a similar (but different) issue see also https://github.com/microsoft/azure-spring-boot/issues/650 -->
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
<version>7.5</version>
</dependency>
Note the workaround regarding the dependency com.nimbusds.
I tried navigating to the relevant code. However, for some reason the code of AzureContextAutoConfiguration.credentials
is not available in the disassembled code of my IDE.
Is the version I used in the workaround in my pom.xml appropriate?
Does anyone have an idea what the error means and how to fix it?
Can anyone report, that this tutorial still works?
[1] https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-cloud-stream-binder-java-app-azure-event-hub
So here the root issue is
"Failed to instantiate
[com.microsoft.azure.credentials.AzureTokenCredentials]: Factory method 'credentials' threw exception;
nested exception is java.lang.IllegalArgumentException: name"
I would suggest you to verify your my.azureauth and application.properties files again against the guide.
Also, as the guide suggests, if you're using JDK version 9 or greater (you are using 11 here), add the following dependencies
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
<scope>runtime</scope>
</dependency>

How to fix problem of spring-boot-devtools with axon?

I have to use spring-boot-devtools & axon in spring-boot application. I have included both of them in pom.xml. It is not working. Application fails to start.
I have tried to run application without using spring-boot-devtools then it works perfectly as expected but not with spring-boot-devtools. I have read document of axon on https://docs.axoniq.io/reference-guide/setting-up/spring-boot where it is suggested not to use devtools with axon. I also refereed issue https://github.com/AxonFramework/AxonFramework/issues/976 where it is stated, removing the devtools dependency did the trick. I don't want to remove devtools as it is useful in development mode. Pom.xml content:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
<version>4.0.3</version>
</dependency>
I expect application to start but got error :
Description:
The bean 'commandBus', defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/axonframework/springboot/autoconfig/AxonServerAutoConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
I have tried spring.main.allow-bean-definition-overriding=true but not working.

Spring Boot and Tomcat not finding JNDI resources

I'm using Spring Boot 1.4.2.RELEASE. I'm also using Tomcat 8.5.9 (NOT embedded) and Java 8.
In my application.properties file, I can manually configure my datasource using:
spring.datasource.url = jdbc:as400://blahblah....
spring.datasource.username = myuser
spring.datasource.password = password
spring.datasource.driver-class-name=com.ibm.as400.access.AS400JDBCDriver
This works just fine. But, we use Tomcat to host our connections. In Tomcat, we have:
<GlobalNamingResources>
<Resource name="jdbc/BLAH" ..... />
</GlobalNamingResources>
So I then changed application.properties to:
spring.datasource.jndi-name=java:comp/env/jdbc/BLAH
spring.jpa.database-platform=org.hibernate.dialect.DB2400Dialect
spring.jpa.show-sql=true
Now, I can't even build the application. I get the following exception:
o.s.w.c.s.GenericWebApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration':
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [javax.sql.DataSource]:
Factory method 'dataSource' threw exception; nested exception is
org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException:
Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/BLAH';
nested exception is javax.naming.NoInitialContextException:
Need to specify class name in environment or system property, or as an applet parameter,
or in an application resource file: java.naming.factory.initial
The NoInitialContextException leads me to believe that Spring Boot cannot find Tomcat running so it can't perform a JNDI lookup.
Keep in mind, the exact same setup worked for us using Spring Boot 1.2.5.RELEASE. We have another app that uses 1.2.5.RELEASE, Java 7 and Tomcat 7 and it works using JNDI resources. But using the newest Spring Boot, Java 8 and Tomcat 8 doesn't.
Any help is appreciated.
Some snippets from my pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/>
</parent>
...
<dependencies>
<dependency>
<groupId>net.sf.jt400</groupId>
<artifactId>jt400</artifactId>
<version>6.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
....
</dependencies>
Other code snippets:
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan(basePackages = "com.company.myapp")
#EnableJpaRepositories(basePackages = "com.company.myapp.repository")
#EnableTransactionManagement
#EnableVaadin
#EnableVaadinServlet
public class AppConfiguration {
}
UPDATE
I've created the resources within Tomcat (external) using the GlobalNamingResources in our server.xml.
This works for other applications.
UPDATE 2
Forgot to mention that I have the following in a src/main/webapp/META-INF/context.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink global="jdbc/BLAH"
name="jdbc/BLAH"
type="com.mchange.v2.c3p0.ComboPooledDataSource"/>
</Context>
UPDATE 3
Also, I want to make it clear the problem happens during the maven package step.
If you are deploying on a standalone Tomcat (Not embedded), I'm fairly sure you need to define your JNDI resources on the Tomcat and not in application.properties, but in server.xml like this:
<GlobalNamingResources>
<Resource name="jdbc/serverdb" auth="Container" type="javax.sql.DataSource"
maxTotal="5" maxIdle="5" maxWaitMillis="30000"
validationQuery="select 1" testOnBorrow="true"
username="$AUTH_DB_USER" password="$AUTH_DB_PASSWORD" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://$AUTH_DB_HOST:5432/$AUTH_DB_NAME"/>
</GlobalNamingResources>

Error loading DispatcherServlet's default strategy class

I m getting the following exception while trying to deploy the war file on Weblogic 10.3.6 server.
The application is using SPring mvc.
Error loading DispatcherServlet's default strategy class [org.springframework.web.servlet.mvc.support.Def
aultHandlerExceptionResolver] for interface [org.springframework.web.servlet.HandlerExceptionResolver]: problem with class file or dependent class; nested exception is
java.lang.NoClassDefFoundError: org/springframework/web/bind/MethodArgumentNotValidException
at org.springframework.web.servlet.DispatcherServlet.getDefaultStrategies(DispatcherServlet.java:766)
at org.springframework.web.servlet.DispatcherServlet.initHandlerExceptionResolvers(DispatcherServlet.java:604)
at org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:423)
at org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:410)
at org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:752)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:989)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:1)
at org.springframework.context.event.GenericApplicationListenerAdapter.onApplicationEvent(GenericApplicationListenerAdapter.java:51)
at org.springframework.context.event.SourceFilteringListener.onApplicationEventInternal(SourceFilteringListener.java:97)
at org.springframework.context.event.SourceFilteringListener.onApplicationEvent(SourceFilteringListener.java:68)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
Your classpath doesn't have spring-web-x.x.x.jar, that's why the exception is. Please use Maven build tool for building your Spring MVC project. Add all the necessary dependencies in pom.xml. For spring-web 3.0.4 version, it would be
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

Categories