标签: java-9

Java 9中的opens指令

我正在阅读Java 9规范的草案,但这句话对我来说并不清楚:

opens指令指定当前模块要打开的包的名称.这使得包中的公共和受保护类型及其公共和受保护成员只能在运行时访问其他模块中的代码.它还可以通过Java SE Platform的反射库访问包中的所有类型及其所有成员.

如果open只在运行时使public和protected可访问,那么通过反射可以访问包区域中所有类型的含义是什么?

我不明白运行时和反射之间的区别.

看起来打开的包只能在运行时访问公共和受保护(通过反射?)以及其他未指定类型的包和可访问的成员(也是私有...).

java java-9

9
推荐指数
1
解决办法
504
查看次数

包javax.jnlp在模块java.jnlp中声明,它不在模块图中

我有一个使用Java webstart技术的项目.我决定将Java版本从8升级到9.但是,我在编译时面临以下错误:

error: package javax.jnlp is not visible
import javax.jnlp.DownloadServiceListener;
        ^
(package javax.jnlp is declared in module java.jnlp, which is not in the module graph)
Run Code Online (Sandbox Code Playgroud)

我试图包括C:\Program Files\Java\jdk-9\lib\javaws.jar在类路径中,但仍然存在同样的问题.

java jnlp java-web-start java-9

9
推荐指数
1
解决办法
2772
查看次数

等于字符串文字和来自NumberFormat的字符串因字节表示不同而失败

我用prod代码玩Java 9.我发现一些格式化测试失败了.经过一些研究,我能够创建一个类来重现这个问题.这种情况发生在Java 9中,而不是Java 8中.

该课程在这里 https://pastebin.com/87sA5WMb

import java.text.*;
import java.util.*;

public class FormatFails {
    public static void main(String... args){

        Currency currency = Currency.getInstance("EUR");
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
        currencyFormatter.setMaximumFractionDigits(0);
        currencyFormatter.setMinimumFractionDigits(0);
        currencyFormatter.setCurrency(currency);

        String expected = "123 457 €";
        String obtained = currencyFormatter.format(123456.789);
        System.out.println(expected);
        System.out.println(obtained);
        System.out.println(expected.equals(obtained));

        System.out.format("Bytes from expected: %s\n",Arrays.toString(expected.getBytes()));
        System.out.format("Bytes from obtained: %s\n",Arrays.toString(obtained.getBytes()));       
    }

}
Run Code Online (Sandbox Code Playgroud)

基本上比较2个字符串:

  • 一个字符串文字和
  • Locale.FRANCE中使用NumberFormat生成的格式化字符串

因为以下原因,在两者上使用equals失败:

123 457 €
123 457 €
false
Bytes from expected: [49, 50, 51, -62, -96, 52, 53, 55, 32, -30, -126, -84]
Bytes …
Run Code Online (Sandbox Code Playgroud)

java java-9

9
推荐指数
1
解决办法
165
查看次数

Java 9 HttpClient发送multipart/form-data请求

以下是表格:

<form action="/example/html5/demo_form.asp" method="post" 
enctype=”multipart/form-data”>
   <input type="file" name="img" />
   <input type="text" name=username" value="foo"/>
   <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

何时提交此表单,请求将如下所示:

POST /example/html5/demo_form.asp HTTP/1.1
Host: 10.143.47.59:9093
Connection: keep-alive
Content-Length: 326
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://10.143.47.59:9093
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryEDKBhMZFowP9Leno
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4

Request Payload
------WebKitFormBoundaryEDKBhMZFowP9Leno
Content-Disposition: form-data; name="username"

foo
------WebKitFormBoundaryEDKBhMZFowP9Leno
Content-Disposition: form-data; name="img"; filename="out.txt"
Content-Type: text/plain


------WebKitFormBoundaryEDKBhMZFowP9Leno--
Run Code Online (Sandbox Code Playgroud)

请注意"请求有效负载",你可以在表单中看到两个参数,用户名和img(form-data; name ="img"; filename ="out.txt"),并且名字是文件系统中的实际文件名(或路径),您将在后端按名称(而不是文件名)接收文件(例如spring controller).
如果我们使用Apache Httpclient来模拟请求,我们将编写这样的代码:

MultipartEntity …
Run Code Online (Sandbox Code Playgroud)

java http multipartform-data http2 java-9

9
推荐指数
3
解决办法
5078
查看次数

Spring:@Resource注入在JDK9下停止工作

在我的@Configuration课堂上,我有如下的家属:

@Configuration
public class MyConfig {
    @Resource(name = "firstDataSource")
    private DataSource firstDataSource;

    // more code
}
Run Code Online (Sandbox Code Playgroud)

在Oracle JDK 8:firstDataSource字段中工作的依赖项注入已成功注入非null值.

现在我尝试在JDK 9中运行应用程序(没有修改).结果是@Resource不再触发依赖注入:使用此注释注释的所有内容仍然存在null.

什么可能是@Resource停止工作的原因?

在项目中使用Spring 4.0.9.

这是一个测试项目,展示了这个问题:https://github.com/rpuch/test-spring-injection-jdk9

它包含一个测试:MainTest我从IDE运行.当我使用JDK 8时,它会输出

??? 29, 2017 10:45:13 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f8ed237: startup date [Fri Sep 29 22:45:13 SAMT 2017]; root of context hierarchy
OK
Run Code Online (Sandbox Code Playgroud)

但是在JDK 9下,它会在启动期间抛出异常,这是由以下原因造成的:

Caused by: java.lang.IllegalStateException: bean1 is not injected
    at Bean2.<init>(Bean2.java:7)
    at Config2.bean2(Config2.java:16)
Run Code Online (Sandbox Code Playgroud)

当没有注入依赖时会发生这种情况.

java spring dependency-injection java-9

9
推荐指数
2
解决办法
2124
查看次数

JShell不会使用Git Bash执行任何操作

我在Windows 上安装了Java9(Oracle网站上当前可用的版本)(Windows 10 x64 Professional,也是最新的,作为vmware虚拟机运行 - > VMware Workstation 12).我添加jdk\bin到用户路径并尝试使用Git Bash执行命令,但即使JShell启动,它也不会在按下后执行任何命令Enter,它只是坐在那里而不做任何事情.

有趣的是,它与Windows的默认命令行完美的作品CMD或Powershell.如果我没记错,Git Bash是基于Cygwin的.这可能是什么原因?

此外,它没有直接连接JShell,但我在一台不同的机器上有一些类似的问题,有一些命令行工具,如JHipster(适用于CMD但不适用于GitBash).

必要的截图: -

在此输入图像描述

编辑:使用jshell -v并没有什么区别.此外,使用任何REPL帮助程序命令,/help或/help intro使shell也停止.

java git-bash java-9 jshell

9
推荐指数
1
解决办法
1022
查看次数

Java 9:模块路径上可能有2个具有相同名称的模块

是否可以在模块路径上拥有2个具有完全相同名称(但内容略有不同)的模块?

据我所知,Java 9编译器并没有抱怨它.我有2个模块声明如下:

module com.dj.helper {
    exports com.dj.helper;
}
Run Code Online (Sandbox Code Playgroud)

两者都包含com.dj.helper包,但在包内的内容是不同的.然后在我的主应用程序中,我想要导入此模块:

module com.dj {
    requires com.dj.helper;
}
Run Code Online (Sandbox Code Playgroud)

具有相同名称的两个模块都在我的模块路径上.

我希望在编译我的com.dj模块时,编译器会抱怨同一模块存在两次,但事实并非如此.这是否有效地意味着您可以在模块路径上拥有相同jar的2个版本,而Java将不知道使用哪个版本?

java java-9 java-module module-info

9
推荐指数
2
解决办法
1495
查看次数

如何在Java 9中获取Process的commandLine和参数

Java 9提供了获取信息的漂亮方式Process,但我仍然不知道如何获取进程的CommandLine&arguments:

Process p = Runtime.getRuntime().exec("notepad.exe E:\\test.txt");
ProcessHandle.Info info = p.toHandle().info();
String[] arguments = info.arguments().orElse(new String[]{});
System.out.println("Arguments : " + arguments.length);
System.out.println("Command : " + info.command().orElse("")); 
System.out.println("CommandLine : " + info.commandLine().orElse(""));
Run Code Online (Sandbox Code Playgroud)

结果:

Arguments : 0
Command : C:\Windows\System32\notepad.exe
CommandLine : 
Run Code Online (Sandbox Code Playgroud)

但我期待:

Arguments : 1
Command : C:\Windows\System32\notepad.exe
CommandLine : C:\Windows\System32\notepad.exe E:\\test.txt
Run Code Online (Sandbox Code Playgroud)

java windows process java-9

9
推荐指数
3
解决办法
553
查看次数

Spring 9中的Java 9拆分包错误

当我在SpringBoot代码中引入java 9模块时,我得到了大量的"拆分包"错误.它们无处不在,例如:

Error:java: the unnamed module reads package org.bson.types from both bson and mongodb.driver
Error:java: the unnamed module reads package org.bson.io from both bson and mongodb.driver
Error:java: the unnamed module reads package org.bson from both bson and mongodb.driver
Error:java: the unnamed module reads package com.mongodb.client.model from both mongodb.driver.core and mongodb.driver
Error:java: the unnamed module reads package com.mongodb.client from both mongodb.driver.core and mongodb.driver
Error:java: the unnamed module reads package com.mongodb from both mongodb.driver.core and mongodb.driver
Error:java: the unnamed module reads package …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot java-9 java-module

9
推荐指数
0
解决办法
653
查看次数

添加到DateTimeFormatterBuilder的Literal破折号导致解析失败

我正在尝试创建一个DateTimeFormatter匹配以下示例(它实际上稍微复杂一些,但这应该无关紧要).

20180302-17:45:21
Run Code Online (Sandbox Code Playgroud)

我写了以下内容但它导致了一个例外:

new DateTimeFormatterBuilder()
    .append(DateTimeFormatter.BASIC_ISO_DATE)
    .appendLiteral('-')
    .append(DateTimeFormatter.ISO_LOCAL_TIME)
    .toFormatter()
    .parse("20180302-17:45:21");
Run Code Online (Sandbox Code Playgroud)

例外是:

Exception in thread "main" java.time.format.DateTimeParseException: Text '20180302-17:45:21' could not be parsed at index 11
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1816)
Run Code Online (Sandbox Code Playgroud)

它似乎是在结肠之间失败17:45,DateTimeFormatterBuilder.appendLiteral并没有提供任何线索.

如果我将文字更改为另一个字符,让我们说m,那么它工作正常:

new DateTimeFormatterBuilder()
    .append(DateTimeFormatter.BASIC_ISO_DATE)
    .appendLiteral('m')
    .append(DateTimeFormatter.ISO_LOCAL_TIME)
    .toFormatter()
    .parse("20180302m17:45:21");
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?假设我无法更改格式,我该如何解决?

评论表明这可能取决于版本.我正在使用JDK 9.0.1,它已在9.0.4上复制.

java java-time java-9

9
推荐指数
1
解决办法
296
查看次数