标签: java-9

如何让Java 9在HiDPI显示器上使用更高分辨率的图像?

我下载并安装了最新的Java 9早期版本,并且很高兴地发现Windows上的Java Swing应用程序现在可以以正确的大小呈现组件.使用HiDPI显示器的客户不再需要以预期宽度和高度的一半使用我的应用程序.

但是我注意到在我的Swing应用程序中,我自己的图标只是缩放到宽度和高度加倍,使它们看起来很锯齿.

我确实拥有正常尺寸(例如foobar.png)和双倍宽度/高度(例如foobar@2x.png)的全套图标.我使用Apple命名约定.

如何让Java 9在可用的情况下轻松找到并使用图像的更高分辨率版本,而无需手动编写代码?

代码示例总是让问题更清晰,Java 9还有一个多分辨率图标类,我可以使用它来编译和运行以下代码吗?

import javax.swing.*;

public class ScratchSpace {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("I work in HiDPI!");

            // can I replace the following line with some magical new method that automatically loads the same icon at multiple resolutions
            // so that Swing chooses the correct one when rendering, depending on the current scale of the current display?
            Icon icon = new IconWithMultipleSizes("foobar.png"); …
Run Code Online (Sandbox Code Playgroud)

java macos swing java-9 hidpi

6
推荐指数
1
解决办法
995
查看次数

jol在Java9下有点破碎吗?

使用java-9 build 9-ea + 149 和jol 0.6.

运行这个简单的代码:

ArrayList<Integer> list = new ArrayList<>();
list.add(12);

System.out.println(ClassLayout.parseInstance(list).toPrintable());
Run Code Online (Sandbox Code Playgroud)

输出:

  OFFSET  SIZE     TYPE DESCRIPTION                    VALUE
  0     4          (object header)                01 00 00 00 (00000001 00000000 00000000 00000000) (1)
  4     4          (object header)                00 00 00 00 (00000000 00000000 00000000 00000000) (0)
  8     4          (object header)                0e 8d 00 f8 (00001110 10001101 00000000 11111000) (-134181618)
 12     4      int AbstractList.modCount          (access denied)
 16     4      int ArrayList.size                 (access denied)
 20     4 Object[] ArrayList.elementData          (access denied)
Run Code Online (Sandbox Code Playgroud)

此访问被拒绝的部分来自方法中的FieldData.java: …

java jol java-9

6
推荐指数
1
解决办法
363
查看次数

Maven 3.5与JDK 9的问题

我正在使用Maven 3.5.0和JDK 9 build 171.当我尝试编译我的项目时,我得到了一个例外:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project admin: Fatal error compiling: ja-va.lang.NoClassDefFoundError: com/sun/tools/javac/file/BaseFileObject: com.sun.tools.javac.file.BaseFileObject 
Run Code Online (Sandbox Code Playgroud)

我向MAVEN_OPTS添加了建议的标志,但没有成功:

--add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
Run Code Online (Sandbox Code Playgroud)

有什么建议?

maven java-9

6
推荐指数
2
解决办法
5114
查看次数

JDK 9:使用SpringExtension进行JUnit 5测试编译会产生java.lang.NoClassDefFoundError:org/w3c/dom/ls/DocumentLS

我认为这个问题与JDK 9中的模块排除无关(与java.se.ee一样),而是JDK 9 在java.xml模块中包含更新版本的org.w3c.dom.ls没有DocumentLS类.

堆栈跟踪的重要位是:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring-test/test-container.xml]; nested exception is java.lang.NoClassDefFoundError: org/w3c/dom/ls/DocumentLS
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
Run Code Online (Sandbox Code Playgroud)

即使我在此类中包含运行时依赖项,例如xerces:xerces 2.4.0,JDK java.xml模块也是首选(我猜).

我正在使用Gradle 4.1.有没有办法限制JDK提供的模块的范围?

spring-test java-9 junit5

6
推荐指数
1
解决办法
2115
查看次数

模块不依赖于Intellij IDEA 2017.2.5 Java 9

我刚刚在命令行中测试了我对java 9模块的理解.然后我转到Intellij IDEA 2017.2.5进行测试.在那里,我面临错误module is not in dependencies不知道为什么intellij显示错误.

我只是在module-info.javaas exports和requires.中编写必需的语句.

然后我使用Intellij智能来解决IDEA的错误.只需ALT + ENTER然后点击Add dependency on module 'module-name-here'.但我不知道Intellij在幕后做了什么.有什么想法吗?

java intellij-idea java-9

6
推荐指数
1
解决办法
1148
查看次数

Java 9 JavaFX Preloader

在Java 8中,我可以使用以下方法使用预加载器启动JavaFX应用程序:

LauncherImpl.launchApplication(WindowMain.class, WindowMainPreloader.class, 
new String[]{...});
Run Code Online (Sandbox Code Playgroud)

我更喜欢从代码开始,如上所述,而不是使用部署配置,因为我不希望每次启动应用程序时都启动图形界面,但只有在计算出应用程序运行的某些代码之后GUI模式.

我使用的是"com.sun.javafx.application.LauncherImpl"类,但显然在Java 9中,所有以"com.sun"开头的类都被删除了.那么,如何在Java 9中使用预加载器启动应用程序?

java javafx preloader java-9 javafx-9

6
推荐指数
1
解决办法
1456
查看次数

在java 9中使用jdeps为自动模块创建module-info

我有3罐杰克逊图书馆

  1. 杰克逊核心2.8.10.jar
  2. 杰克逊的注解 - 2.8.0.jar
  3. 杰克逊 - 数据绑定,2.8.10.jar

我成功地为core和annotation创建了module-info.java,并使用jdeps将它们转换为Named maodule.

对于数据绑定,我尝试了以下命令:

jdeps --generate-module-info.--module-path%JAVA_HOME%\ jomds; jackson.core; jackson.annotations existingmods\jackson-databind-2.8.10.jar

现在发生以下错误:

Missing dependence: .\jackson.databind\module-info.java not generated
Error: missing dependencies
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonCreator       not found
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonCreator$Mode  not found
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonFormat        not found
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonFormat$Value  not found
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonIgnoreProperties not found
   com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonIgnoreProperties$Value not found.
Run Code Online (Sandbox Code Playgroud)

如何为jackson-databind生成module-info.java?

jar java-9 jdeps java-module

6
推荐指数
1
解决办法
6275
查看次数

接口中的Java 9默认方法:从哪个模块调用它们?

假设你有moduleA和moduleB.ModuleA定义了一个接口(例如用于服务),ModuleB有一个实现接口的具体类(提供服务).

现在,如果接口有一个默认方法,并且你在moduleB中的类(来自另一个模块)上调用它,那么这个调用应该在moduleA或moduleB中执行吗?显然它来自moduleA ......理由是什么?

示例:假设您有一个执行此操作的代码:

InputStream is = this.getClass().getResourceAsStream(fullPath);
Run Code Online (Sandbox Code Playgroud)

如果此代码位于moduleB中服务的实现中,则将打开流.但是如果代码位于moduleA中的默认方法中,那么当在moduleB上调用服务时,您需要在moduleB中拥有一个"open"资源(因此看起来调用认为它来自"outside"moduleB).

我想了解一下原因.

谢谢


用一个例子编辑我的问题.假设你在moduleA中有这段代码:

public interface PropertiesProvider {
    public default Properties get(String domain) {
        Class clazz =this.getClass();
        System.out.println(" CLASS " +clazz);
        InputStream is = clazz.getResourceAsStream(domain) ;
        if (is != null) {
            Properties props = new Properties();
            try {
                props.load(is);
                return props;
            } catch (IOException e) {
                //log
            }
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

}

在moduleB中

public class PropertiesProviderImpl implements PropertiesProvider {}
Run Code Online (Sandbox Code Playgroud)

如果您从ModuleA调用该服务,则调用该调用来自类PropertiesProviderImpl查找资源但如果它未被"打开"则不加载它

如果将代码复制到PropertiesProviderImpl中,则调用会追溯到同一个类找到ressource并加载它,即使它没有"打开"

所以我的问题是:为什么调用后的差异来自同一个类?(不同之处在于,在一种情况下,该方法是从接口中的默认方法继承的类型)

java interface java-9

6
推荐指数
1
解决办法
215
查看次数

ProcessHandle onExit具有空数据

我目前正在测试java 9中的Process API,我对以下代码有一些问题:

Process process = new ProcessBuilder( List.of("ping", "-i", "1", "-c", "4", "google.com")).start();

CompletableFuture<Void> startTimeFuture = process.toHandle().onExit()
            .thenApply(ProcessHandle::info)
            .thenApply(ProcessHandle.Info::startInstant)
            .thenAccept(System.out::println);

startTimeFuture.get();
Run Code Online (Sandbox Code Playgroud)

当我执行这个片段时,我在终端中获得Optional.empty.Javadoc声明该info方法返回任何数据(如果可用),因此我怀疑JVM无法获取有关生成进程的信息.但是当我试图从ProcessHandle未来获得pid时,我得到了适当的价值.

总结一下,我的问题是:

ProcessHandle.Info打电话后有没有办法让非空onExit()?

我正在使用Ubuntu 16.04 LTS

编辑 - 执行时这是终端的输出ping -i 1 -c 5 google.com

PING google.com(xxx.xxx.16.46)56(84)字节的数据.

来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 1 ttl = 52 time = 6.71 ms

来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 2 ttl = 52 time = 6.26 ms

来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 3 ttl = 52 time = 16.6 ms

来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64字节:icmp_seq = 4 ttl …

java java-9 processhandle

6
推荐指数
1
解决办法
218
查看次数

功能样式java.util.regex匹配/组提取

使用java.util.regex提取子字符串,我发现自己实现了相同的代码模式,以解决对的调用:

Pattern p = Pattern.compile(pattern); // can be static final
Matcher m = p.matcher(input);
if (m.find()) { // or m.matches()
    foo(m.group(x));
} else {
    ...
}
Run Code Online (Sandbox Code Playgroud)

是否有功能扩展或流行的库(番石榴/ apache通用),避免了不必要的,容易出错的局部变量,例如:

Pattern p = Pattern.compile(pattern); // can be static final
p.matchedGroup(input, x) // return Optional<String>
    .map(group -> foo(group))
    .orElse(...);
Run Code Online (Sandbox Code Playgroud)

以及一系列匹配结果,例如:

Pattern p = Pattern.compile(pattern);
p.findMatches(input)
    .map((MatchResult mr) -> {
        mr.groupsIfPresent(x).map(g -> foo(g)).orElse(...)
    })
    .findFirst();
Run Code Online (Sandbox Code Playgroud)

似乎Java8中唯一的功能性添加是,.splitAsStream()但是仅在尝试拆分匹配项时才有用。

java regex java-9

6
推荐指数
2
解决办法
128
查看次数