gpt4 book ai didi

java - ClassNotFoundException : com. mysql.jdbc.Driver gradle 无法解析

转载 作者:行者123 更新时间:2023-11-29 03:27:23 26 4
gpt4 key购买 nike

我正在使用 IntelliJ IDEA 14.1.5

我已经设置了 gradle 项目,我正在为我的数据库使用 mysql。

我在build.gradle文件中写了依赖:

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'mysql:mysql-connector-java:5.1.23'
runtime 'mysql:mysql-connector-java:5.1.23'
}

我从 gradle 工具中刷新了所有项目。

在外部库中有:Gradle: mysql:mysql-connector-java:5.1.23

在项目结构中,在 Libraries 中也存在 Gradle:mysql:mysql-connector-java:5.1.23 exists

编译后出现错误

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at authentication.ServletListener.contextInitialized(ServletListener.java:76)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1678)
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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:463)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:413)
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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1470)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1311)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1403)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:832)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$255(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/1878994965.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

可能是什么问题,我该如何解决?我其实对gradle不熟悉,所以请你帮忙解决这个问题好吗?


我正在添加调用 mysql 依赖项的代码。没有什么特别的。它是 servletContextListener 类的方法。 ContextInicialized 方法是创建连接的方法

    public ServletListener() throws FileNotFoundException {
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(configFileName));
} catch (IOException e) {
e.printStackTrace();
}
dbServer = properties.getProperty("db_server");
dbUser = properties.getProperty("db_user");
dbPassword = properties.getProperty("db_pass");
dbName = properties.getProperty("db_name");
dbName = properties.getProperty("db_name");
}

public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://"+dbServer,dbUser,dbPassword);
BaseManager baseManager = new BaseManager(connection, dbName);
servletContext.setAttribute("baseManager",baseManager);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

最佳答案

使用新的 gradle 任务将 jdbc 驱动程序放入您将创建的 jar 中。并且不要忘记在 dependencies 中设置 compile 组,当然还有您的主类和工具名称以及您的版本:

version = '0.3.0'

task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'My Tool',
'Implementation-Version': version,
'Main-Class': 'de.test.mypackage.MainClass'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

dependencies {
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.21'
}

打电话

gradle fatJar

构建 build/libs/project.name-all-VERSION.jar

关于java - ClassNotFoundException : com. mysql.jdbc.Driver gradle 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com