繁体   English   中英

如何在线程“ main”中修复异常java.lang.NoClassDefFoundError:com / itextpdf / text / DocumentException

[英]How to fix Exception in thread “main” java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentException

我创建了一个控制台应用程序。 这个程序可以生成一个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中启动我的应用程序时,它可以正确运行。

build.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

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

您有两个主要选择

  • 使用java -cp <path/to/itext.jar> yourprogram启动程序
  • 使用gradle构建一个“胖jar”并运行它(胖jar将您的代码及其所有依赖项捆绑到一个可执行的jar文件中)

暂无
暂无

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

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