gpt4 book ai didi

java - 如何修复线程 "main"java.lang.NoClassDefFoundError : com/itextpdf/text/DocumentException中的异常

转载 作者:行者123 更新时间:2023-12-01 08:51:02 27 4
gpt4 key购买 nike

我创建了一个控制台应用程序。这个应用程序可以生成pdf。我用的是itextpdf。我在构建gradle中添加了:

compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.10'

当我在命令行中启动程序时,我看到以下日志:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.text.DocumentException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more

当我在 IntelliJ 中启动我的应用程序时,它可以正常工作。

构建.gradle:

      group 'Harmonogramy'
version '1.0-SNAPSHOT'

task wrapper(type: Wrapper) {
gradleVersion = '3.1'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.34'
// https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils
compile group: 'commons-dbutils', name: 'commons-dbutils', version: '1.6'
// https://mvnrepository.com/artifact/org.apache.commons/commons-csv
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'
// https://mvnrepository.com/artifact/com.itextpdf/itextpdf
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.10'
// https://mvnrepository.com/artifact/net.sf.opencsv/opencsv
compile group: 'net.sf.opencsv', name: 'opencsv', version: '2.3'

}

jar {
archiveName = 'Harmonogramy.jar'

manifest {
attributes 'Main-Class': 'Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': version
}

from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) })
}

最佳答案

您需要将库与应用程序打包在一起,使其可运行并具有运行时所需的所有依赖项。例如在你的 build.gradle 中

在查看你的代码时,我有一种感觉,你没有遵循 src/main/java 结构,因此 gradle 不知道从哪里获取源文件,因为默认是 src/main/java 。您可以修改 SourceSets,但我建议遵循结构约定。

apply plugin: 'java'

sourceCompatibility = 1.5
targetCompatibility = 1.5

version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
compile 'mysql:mysql-connector-java:5.1.34'
compile 'commons-dbutils:commons-dbutils:1.6'
compile 'org.apache.commons:commons-csv:1.4'
compile 'com.itextpdf:itextpdf:5.5.10'
compile 'net.sf.opencsv:opencsv:2.3'
testCompile 'junit:junit:4.11'
}

jar {
archiveName = 'Harmonogramy.jar'

manifest {
attributes 'Main-Class': 'Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': version
}
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) })
}

jar 任务将扩展默认的 gradle 任务来构建一个 jar,并将所有编译依赖项打包在其中 - 请记住指出您的主类以使其可执行。

或者您可以尝试使用此处找到的影子插件 https://github.com/johnrengelman/shadow

关于java - 如何修复线程 "main"java.lang.NoClassDefFoundError : com/itextpdf/text/DocumentException中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409981/

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