当前位置: 首页 > 知识库问答 >
问题:

如何修复异常在线程"main"java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentExctive

葛炯
2023-03-14

我创建了一个控制台应用程序。这个应用程序可以生成pdf。我使用itextpdf。我在build 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中启动应用程序时,它会正常工作。

建筑格雷德尔:

      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) })
}

共有2个答案

纪鸿禧
2023-03-14

控制台应用程序需要在运行时访问itext jar。IntelliJ自动提供这种依赖关系,但如果您从控制台运行代码,则由您提供依赖关系。

你有两个主要选择

  • java-cp启动程序

澹台阳秋
2023-03-14

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

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

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

 类似资料: