- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
短版:我正在尝试从 gradle 任务调用 jpackage,但 ToolProvider 返回 null(或者更好的是失败的 Optional)。 AdoptOpenJDK 14.0.0(sdkman 标识符 14.0.0.hs-adpt)和 Java.net(我认为那是 Oracle OpenJDK!?)14.0.1(sdkman 标识符 14.0.1-open)就是这种情况。我正在使用 Gradle 6.3(但这感觉不像是 gradle 问题)。
长版:我正在关注 talk on jpackage ,在 12:12 显示从构建工具调用 jpackage 的代码。 (official jpackage page 还提到:除了命令行界面,jpackage 还可以通过名为“jpackage”的 ToolProvider API (java.util.spi.ToolProvider) 访问。)
仍然是我的(Kotlin)代码(位于 buildSrc/src/main/kotlin 中)
object JPackage {
fun buildInstaller(
...
): Int {
val jpackageTool: ToolProvider = ToolProvider.findFirst("jpackage").orElseThrow {
val javaVersion: String = System.getProperty("java.version")
IllegalStateException(
"jpackage not found (expected JDK version: 14 or above, detected: $javaVersion)"
)
}
val arguments: Array<String> = ...
return jpackageTool.run(System.out, System.err, *arguments)
}
}
tasks {
register("buildInstaller") {
JPackage.buildInstaller(
...
)
dependsOn("build")
}
}
> Could not create task ':buildInstaller'.
> jpackage not found (expected JDK version: 14 or above, detected: 14.0.1)
public class Main {
public static void main(String[] args) {
java.util.spi.ToolProvider.findFirst("jpackage").orElseThrow(() -> {
String javaVersion = System.getProperty("java.version");
return new IllegalStateException("jpackage not found (expected JDK version: 14 or above, detected: " + javaVersion + ")");
});
System.out.println("success");
}
}
object JPackage {
fun buildInstaller(
...
): Int {
val arguments: Array<String> = ...
return execJpackageViaRuntime(arguments)
}
private fun execJpackageViaRuntime(arguments: Array<String>): Int {
val cmdAndArgs = ArrayList<String>(arguments.size+1).let {
it.add("jpackage")
it.addAll(arguments)
it.toTypedArray()
}
return try {
val process: Process = Runtime.getRuntime().exec(cmdAndArgs)
process.waitFor(3L, TimeUnit.MINUTES)
return process.exitValue()
} catch (e: Exception) {
1
}
}
}
tasks {
register("buildInstaller") {
dependsOn("build")
doLast {
if (JavaVersion.current() < JavaVersion.VERSION_14) {
throw GradleException("Require Java 14+ to run 'jpackage' (currently ${JavaVersion.current()})")
}
JPackage.buildInstaller(
...
)
}
}
}
最佳答案
问题与 JEP 11: Incubator Modules 有关.该 JEP 声明:
An incubator module is identified by the
jdk.incubator.
prefix in its module name, whether the module exports an incubating API or contains an incubating tool.
jdk.incubator.jpackage
的模块中。 .从名称的前缀中,我们可以看出该模块是一个孵化器模块。同一个 JEP 后来指出:
Incubator modules are part of the JDK run-time image produced by the standard JDK build. However, by default, incubator modules are not resolved for applications on the class path. Furthermore, by default, incubator modules do not participate in service binding for applications on the class path or the module path [emphasis added].
Applications on the class path must use the
--add-modules
command-line option to request that an incubator module be resolved. Applications developed as modules can specifyrequires
orrequires transitive
dependences [sic] upon an incubator module directly. (Some incubator modules, such as those offering command-line tools, may forego exporting any packages, and instead provide service implementations so that tools can be accessed programatically [sic]. It is usually not recommended to require a module that exports no packages, but it is necessary in this case in order to resolve the incubator module, and have its service providers participate in service binding.)
jdk.incubator.jpackage
模块在运行时未解析。不幸的是,这意味着无法通过
ToolProvider
找到该工具。除非您:
--add-modules jdk.incubator.jpackage
启动 Java 进程, requires jdk.incubator.jpackage;
模块信息文件中的指令。 org.gradle.jvmargs
系统属性。例如:
./gradlew "-Dorg.gradle.jvmargs=--add-modules=jdk.incubator.jpackage" buildInstaller
您不必每次都在命令行中键入它。查看
this Gradle documentation查看您可以在哪里定义该系统属性。可能也可以通过您的 IDE 做某事,但不确定。
jpackage
作为使用
Exec
的另一个进程任务。例如:
tasks {
val build by existing
register<Exec>("buildInstaller") {
if (JavaVersion.current() < JavaVersion.VERSION_14) {
throw GradleException("Require Java 14+ to run 'jpackage'")
}
dependsOn(build)
executable = "jpackage"
args(/* your args */)
}
}
关于java - 无法通过 ToolProvider 找到 jpackage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61307077/
短版:我正在尝试从 gradle 任务调用 jpackage,但 ToolProvider 返回 null(或者更好的是失败的 Optional)。 AdoptOpenJDK 14.0.0(sdkma
我目前正在尝试在运行时编译和执行代码。在我的 IDE (Netbeans) 中,一切都运行得很好,但是当我将代码导出为 .jar 文件时,ToolProvider.getSystemJavaCompi
好像不能用javax.tools.ToolProvider根据 Ant 或 Webstart 的要求从自定义类加载器:http://bugs.sun.com/view_bug.do?bug_id=65
我正在使用 JDK6 编译来自 UI 的输入 src 并立即运行。 但问题是默认情况下我的应用程序使用的是 jre 而不是 jdk,所以: JavaCompiler compiler = ToolPr
我正在尝试加载在运行时生成编译的类。我可以使用这段代码毫无问题地编译它们: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
我阅读了有关此问题的所有帖子,但没有解决方案适合我,我总是 null。 我使用 JRE 并将 tools.jar 放在 lib 目录中,将其添加到构建路径中,但是当我想跳转到声明时,Eclipse 想
我的用例是使用 JDK 6 中提供的 ToolProvider 和 JavaCompiler 类从 java 程序编译生成的源文件。源文件包含对上下文类加载器(它在 J2EE 容器中运行)中的类的引用
我在 Eclipse 中的项目正在运行 WITH SYSTEM JRE 9。当我尝试将 Java 主页设置为 JDK 9 以获取系统编译器时,我得到了 null。 System.setProperty
我正在尝试使用 JavaCompiler 类: http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html 当我调用
我是一名优秀的程序员,十分优秀!