【发布时间】:2020-12-06 02:38:50
【问题描述】:
我最近切换到在我的游戏中使用 jdk11,突然当我导出到一个可运行的 jar 时,我在 build.gradle 文件中放入的任何依赖项都不存在于 jar 中。 切换回 jdk8 可解决此问题。我怎样才能解决这个问题?另外,当您通过 eclipse 导出时,为什么它适用于 8 而不是 11?
我的项目是一个 gradle 项目,手动将依赖项添加到项目设置中,因为外部 jar 似乎可以工作,但我宁愿不这样做。
目前使用的是eclipse 2020-09版本
build.gradle:
apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/", "../client-core/assets/" ]
project.ext.mainClassName = "com.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../client-core/assets");
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task debug(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
debug = true
}
task dist(dependsOn: classes, type: Jar) {
from files(sourceSets.main.output.classesDirs)
from files(sourceSets.main.output.resourcesDir)
from {configurations.compile.collect {zipTree(it)}}
from files(project.assetsDir);
manifest {
attributes 'Main-Class': project.mainClassName
}
}
// TODO. dist still generates a small jar with "build", and the full jar with "release".
// it should generate no jar unless called directly with release.
eclipse {
project {
name = "client-launcher"
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/client-core/assets'
}
}
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
doLast {
def classpath = new XmlParser().parse(file(".classpath"))
new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
def writer = new FileWriter(file(".classpath"))
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(classpath)
}
}
【问题讨论】:
-
在您的 build.gradle 文件中添加
jar { manifest {attributes "Main-Class": "your main class" } from {configurations.compile.collect { it.isDirectory()? it : zipTree(it) } }并运行 clean/build。在 \build\libs 文件夹中会出现一个 jar。那是你的可运行 jar。 -
我会试一试,但是从 Eclipse 导出时它是否适用于 jdk8 而不是 jdk11 有什么原因吗?
标签: java eclipse java-8 executable-jar java-11