简体   繁体   English

"在 Gradle 问题中使用 MySQL JDBC 驱动程序?"

[英]Using MySQL JDBC driver inside Gradle issue?

I am trying to learn how to use Gradle with Java and am starting a brand new project.我正在尝试学习如何将 Gradle 与 Java 一起使用,并且正在开始一个全新的项目。 I am trying to connect to a database.我正在尝试连接到数据库。

My build.gradle<\/code> file is我的build.gradle<\/code>文件是

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': 'com.brian.Application'
    }
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    implementation 'mysql:mysql-connector-java:8.0.25'
}

group 'org.brian'
version '1.0-SNAPSHOT'

This is because Java does not know where to load the MySQL driver class from.这是因为 Java 不知道从哪里加载 MySQL 驱动程序类。 The JAR created by your build does not have a Class-Path<\/code> header that mentions the JAR files (like the MySQL Connector\/J JAR) that it needs to run.由您的构建创建的 JAR 没有提及它需要运行的 JAR 文件(如 MySQL 连接器\/J JAR)的Class-Path<\/code>标头。

You could<\/em> try and configure jar<\/code> to populate Class-Path<\/code> to cite the required JARs.可以<\/em>尝试配置jar<\/code>以填充Class-Path<\/code>以引用所需的 JAR。 Here is an example that works with Gradle 7.3.3<\/code>这是一个适用于 Gradle 7.3.3<\/code>的示例

jar {
    manifest {
        attributes(
                "Main-Class": "com.brian.Application",
                'Class-Path': configurations.runtimeClasspath.files.collect { it.name }.join(' ')
        )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM