[英]How to fix Exception in thread “main” java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentException
I created a console application. 我创建了一个控制台应用程序。 This app can generate a pdf.
这个程序可以生成一个PDF。 I use itextpdf.
我使用itextpdf。 I added in build gradle:
我添加了构建gradle:
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.10'
When I start my program in command line I see a this log: 当我在命令行中启动程序时,看到以下日志:
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
When I start my app in IntelliJ it works corretly. 当我在IntelliJ中启动我的应用程序时,它可以正确运行。
build.gradle : 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) })
}
You need to pack your libraries with your app to make it runnable and have all dependencies needed on runtime. 您需要将库与应用程序打包在一起,以使其可运行并具有运行时所需的所有依赖关系。 In example in your build.gradle
例如在您的build.gradle中
As well looking into your code I have a feeling that you don't follow the src/main/java structure, therefore gradle doesn't know where to get the source files from as the default is src/main/java. 在查看您的代码时,我有一种感觉,您不遵循src / main / java结构,因此gradle不知道从哪里获取源文件,因为默认值为src / main / java。 you can amend SourceSets but I suggest to just follow the structure convention.
您可以修改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) })
}
the jar task will extend the default gradle task to build a jar, and will pack all your compile dependencies within - remember to point your main class out to make it executable. jar任务将扩展默认的gradle任务以构建一个jar,并将所有编译依赖打包在其中-记住指出您的主类使其可执行。
Or you can try to use a shadow plugin found here https://github.com/johnrengelman/shadow 或者您可以尝试使用此处找到的影子插件https://github.com/johnrengelman/shadow
The console application needs access to the itext jar at runtime. 控制台应用程序需要在运行时访问itext jar。 IntelliJ supplies this dependency automatically, but if you run the code from a console, it's up to you to supply the dependency.
IntelliJ自动提供此依赖关系,但是如果您从控制台运行代码,则由您来提供依赖关系。
You have two main choices 您有两个主要选择
java -cp <path/to/itext.jar> yourprogram
java -cp <path/to/itext.jar> yourprogram
启动程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.