我创建了一个控制台应用程序。这个应用程序可以生成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中启动我的应用程序时,它可以正常工作。
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) })
}
答案 0 :(得分:1)
您需要使用应用程序打包库以使其可运行并且在运行时需要所有依赖项。在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
答案 1 :(得分:0)
控制台应用程序需要在运行时访问itext jar。 IntelliJ自动提供此依赖关系,但如果您从控制台运行代码,则由您来提供依赖关系。
您有两个主要选择
java -cp <path/to/itext.jar> yourprogram