使用IDEA-EAP进行JDK9开发实验.
我收到以下错误 -
Run Code Online (Sandbox Code Playgroud)Error:(3, 20) java: package jdk.internal.misc is not visible (package jdk.internal.misc is declared in module java.base, which does not export it to module com.jigsaw.npe)
类定义如下 -
package experiment;
import jdk.internal.misc.Unsafe;
public class CompareAndSwap {
static Unsafe UNSAFE = Unsafe.getUnsafe();
...
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试module-info.java
在使用IDE创建的模块中包含一个文件,其中包含以下语句 -
module com.jigsaw.npe {
requires java.base;
}
Run Code Online (Sandbox Code Playgroud)
目录结构现在看起来如图所示 -
IDE虽然反映了module-info.java
未使用的,但可能这就是我无法定义module com.jigsaw.npe
上面尝试过的原因.
寻求一些帮助,了解如何正确放置module-info.java和/或除了我错过的以外的任何东西.
java java-platform-module-system java-9 java-module module-info
在将我们的一个项目迁移到Java 9(build 9 + 181)时,我遇到了一个特殊的问题,在类型推断和java模块相关的某些库中看起来像是一个不正确的实现.我使用的是dropwizard-core(1.1.0)
和guice(4.1.0)
配置如下:
public class CustomService extends io.dropwizard.Application<CustomServiceConfig> {
public static void main(String[] args) throws Exception {
new CustomService().run(args);
}
// other initializations
@Override
public void run(CustomServiceConfig config, io.dropwizard.setup.Environment environment) throws Exception {
com.google.inject.Injector injector = createInjector(config, environment);
environment.jersey().register(injector.getInstance(SomeResource.class)); //line 45
environment.healthChecks().register("DBHealth", injector.getInstance(HealthCheck.class));
environment.servlets().addFilter("Filter-Name", SomeFilter.class)
.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
private com.google.inject.Injector createInjector(CustomServiceConfig config, Environment environment) {
return com.google.inject.Guice.createInjector(new CustomServiceModule(config, environment));
}
}
Run Code Online (Sandbox Code Playgroud)
public class CustomServiceModule extends com.google.inject.AbstractModule { …
Run Code Online (Sandbox Code Playgroud) 从Java-8开始,我知道类加载器的层次结构如下: -
Bootstrap类加载器 - >扩展类加载器 - >应用程序类加载器
Java 9中类加载器层次结构的变化是什么?它是如何工作的?
我已经看过几篇在Java 9中简要提到自包含应用程序的在线演示文稿,但我有一个问题需要我解决.
使用新模块系统,您现在只允许包含运行应用程序所需的最少代码.但是,希望运行应用程序的系统是否仍然需要JRE,或者是否可以包含在程序中的基本模块中?
我怀疑它是后者,因为下载最新版Java 的页面(这里)仍显示版本8_151.
TL; DR - 使用Java 9,是否可以创建一个可以在没有安装JRE/Java的系统上执行的自包含可执行文件?
假设我有一些lib.jar
我没有源代码的库(或者它是用一些不熟悉模块的非Java语言编写的).lib.jar
没有module-info.class
,我不想将它用作自动模块,所以我想注入module-info.class
它.
我首先module-info.java
使用以下命令生成:
jdeps --generate-module-info . lib.jar
Run Code Online (Sandbox Code Playgroud)
假设这产生了类似的东西:
module lib {
exports package1;
exports package2;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试编译它但javac
失败因为包package1
而且package2
不存在:
> javac module-info.java
module-info.java:4: error: package is empty or does not exist: package1
Run Code Online (Sandbox Code Playgroud)
当然,我可以在其中创建目录package1
和package2
虚拟类,但是有更好的方法吗?
我正在尝试使用java 9和gradle的spring boot.我无法运行我的简单代码,我得到以下提到的错误: -
Information:java: Errors occurred while compiling module 'Java9Gradle_main'
Information:javac 9-ea was used to compile java sources
Information:6/9/2017 10:40 PM - Compilation completed with 65 errors and 0 warnings in 15s 200ms
Error:java: module reads package org.apache.commons.logging from both jcl.over.slf4j and commons.logging
Error:java: module reads package org.apache.commons.logging.impl from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.starter.web reads package org.apache.commons.logging from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.starter.web reads package org.apache.commons.logging.impl from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.autoconfigure reads …
Run Code Online (Sandbox Code Playgroud) Java的ServiceLoader
类现在已经被正式编入Java语言.META-INF/services
现在可以使用.而不是在寻找提供商
provides <spiClass> with <providerClass>
Run Code Online (Sandbox Code Playgroud)
我无法理解的是,uses
在服务加载模块声明中的使用:
uses <spiClass>
Run Code Online (Sandbox Code Playgroud)
引用模块系统的状态
模块系统可以通过扫描模块工件中的类文件来识别
ServiceLoader::load
方法的调用来识别服务的使用 ,但这既缓慢又不可靠.模块使用特定服务是该模块定义的一个基本方面,因此为了效率和清晰度,我们在模块的声明中用uses子句表示:module java.sql { requires transitive java.logging; requires transitive java.xml; exports java.sql; exports javax.sql; exports javax.transaction.xa; uses java.sql.Driver; }
为什么模块系统了解特定服务的使用是否至关重要,尤其是如何引入效率?是不是懒洋洋地加载服务?为什么服务加载器不能直接寻找提供商?
如何通过Java代码获取当前JVM实例中所有模块的列表?可能吗?如果是,那怎么样?
我花了一些时间将我用Groovy编写的项目迁移到Java 10.现在可以编译并运行它.但它仍然没有使用Java 9模块化的任何好处.
谷歌搜索Groovy和Java 9模块几乎没有.
那么是否可以迁移Groovy项目以使用带有Project Jigsaw模块的JDK 10?
我正在尝试使用Spring Boot 2进行Java 10开发,但我遇到了一些问题.
该应用程序是一个基于Spring Boot 2的简单webapp.应用程序启动是可以的但是当我停止它时,我收到此警告:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (file:/C:/Users/CS/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.11/tomcat-embed-core-9.0.11.jar) to field java.io.ObjectStreamClass$Caches.localDescs
WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoaderBase
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经将嵌入式Tomcat服务器从版本8切换到9.0.11以符合Java模块系统.该应用程序随选项启动--add-opens java.base/java.lang=ALL-UNNAMED
有人知道我为什么收到这条消息吗?
java-module ×10
java ×9
java-9 ×9
module-info ×3
java-platform-module-system ×2
spring-boot ×2
classloader ×1
gradle ×1
groovy ×1
java-10 ×1
java-8 ×1
maven ×1
spring ×1
tomcat9 ×1