我刚刚安装了 Netbeans 12.0。当我开始我的第一个项目时,我在创建一个新的 Java 类后收到了一条意外的错误消息。关于我如何处理它的任何建议?
这是消息:
java.lang.IllegalAccessError: superclass access check failed: class org.netbeans.lib.nbjavac.services.CancelAbort (in unnamed module @0x439ece0a) cannot access class com.sun.tools.javac.util.Abort (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x439ece0a
Run Code Online (Sandbox Code Playgroud)
这是我的系统信息:
Product Version = Apache NetBeans IDE 12.0
Operating System = Windows 7 version 6.1 running on amd64
Java; VM; Vendor = 16.0.1; Java HotSpot(TM) 64-Bit Server VM 16.0.1+9-24; Oracle Corporation
Runtime:Java(TM) SE Runtime Environment 16.0.1+9-24
Java Home:c:\program files\java\jdk-16.0.1
System Locale Encoding: pt_BR (nb); Cp1252 …Run Code Online (Sandbox Code Playgroud) 在 Java 11 中引入了 curve25519 内置实现。由于我对此一无所知,并且最近才发现它,因此我使用的是 Signal 的库。这是我在切换到 Java 11 的实现之前的代码:
private final Curve25519 CURVE_25519 = Curve25519.getInstance(Curve25519.JAVA);
public Curve25519KeyPair calculateRandomKeyPair() {
return CURVE_25519.generateKeyPair();
}
public byte[] calculateSharedSecret(byte[] publicKey, byte[] privateKey) {
return CURVE_25519.calculateAgreement(publicKey, privateKey);
}
Run Code Online (Sandbox Code Playgroud)
这是我现在的代码:
private final String XDH = "XDH";
private final String CURVE = "X25519";
@SneakyThrows
public KeyPair calculateRandomKeyPair() {
return KeyPairGenerator.getInstance(CURVE).generateKeyPair();
}
@SneakyThrows
public byte[] calculateSharedSecret(byte[] publicKeyBytes, XECPrivateKey privateKey) {
var paramSpec = new NamedParameterSpec(CURVE);
var keyFactory = KeyFactory.getInstance(XDH);
var publicKeySpec = new XECPublicKeySpec(paramSpec, …Run Code Online (Sandbox Code Playgroud) 有谁知道为什么正则表达式\p{Cs}与Java 16中的符号不匹配?它曾经在 Java 11 中匹配它。
爪哇 11
jshell
| Welcome to JShell -- Version 11.0.7
| For an introduction type: /help intro
jshell> import java.util.regex.*
jshell> var text = new StringBuilder().appendCodePoint(55622).appendCodePoint(56380)
text ==>
jshell> Pattern.compile("\\p{Cs}").matcher(text).find()
$3 ==> true
Run Code Online (Sandbox Code Playgroud)
爪哇 16
INFO: Created user preferences directory.
| Welcome to JShell -- Version 16.0.1
| For an introduction type: /help intro
jshell> import java.util.regex.*
jshell> var text = new StringBuilder().appendCodePoint(55622).appendCodePoint(56380)
text ==>
jshell> Pattern.compile("\\p{Cs}").matcher(text).find()
$3 ==> false
Run Code Online (Sandbox Code Playgroud) 尝试使用record和记录组件的一些代码。我正在使用变量 rarity 组件,并且在自定义构造函数上遇到了编译时错误。
public record Break<R extends Record>(R record, String... notifications) {
public Break(R record, String... notifications) {
System.out.println("record: " + record + " and notifications: " + Arrays.toString(notifications));
this.record = record;
this.notifications = notifications;
}
// compile error: non canonical record constructor must delegate to another costructor
public Break(R record) {
System.out.println("record: " + record);
this.record = record;
}
public Break() {
this(null); // this works
// actually intelliJ suggests it uses the constructor that is not …Run Code Online (Sandbox Code Playgroud) 我刚刚将 Java 从 JDK-15 升级到 JDK-16,在Date使用SimpleDateFormat. 仅使用 9 月份进行格式化时yyyy-MMM-dd就给出了 4 个字符而不是 3 个字符。
例如: 2021-Sep-11显示为2021-Sept-11
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 150);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MMM-dd");
System.out.println(cal.getTime());
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
Run Code Online (Sandbox Code Playgroud)
对我来说看起来像一个错误。我在发行说明中看不到对此的任何更新。有人面临类似的问题吗?一直工作到 JDK-15。
在这篇关于可序列化记录的文章中指出
反序列化通过调用记录类的规范构造函数来创建新的记录对象,将从流中反序列化的值作为参数传递给规范构造函数。这是安全的,因为这意味着记录类可以在将值分配给字段之前对其进行验证,就像普通 Java 程序通过 new 创建记录对象一样。“不可能”的对象是不可能的。
这与仅用于验证的构造函数争论。然而,当构造函数操作参数时,这会导致相当奇怪的行为。考虑这个非常人为的简单示例:
以下记录a在保存之前进行操作:
import java.io.Serializable;
public record TRecord (int a) implements Serializable {
public TRecord {
a = a-1;
}
}
Run Code Online (Sandbox Code Playgroud)
下面的程序只是在第一次保存序列化记录并在随后的时间加载它:
import java.io.*;
public class TestRecords {
public static void main(String args[]) {
TRecord a1 = null;
try {
FileInputStream fileIn = new FileInputStream("tmp");
ObjectInputStream in = new ObjectInputStream(fileIn);
a1 = (TRecord) in.readObject();
in.close();
fileIn.close();
} catch (IOException | ClassNotFoundException i) {
// ignore for now
}
if (a1 == …Run Code Online (Sandbox Code Playgroud) 我们的交互式数学应用程序是使用 Swing JFrames 设计的,并针对 2560p (Mac) 和 1920p (Win) 显示器开发,即使用本机像素坐标进行图形输出和鼠标输入处理。
iMac Retina 5K 测试——大惊喜!应用程序窗口的正常宽度是屏幕宽度的 1/2。我完全预料到在高分辨率 (5120p) iMac(1/4 屏幕宽度)上运行时会出现问题。但不,它呈现 1/2 屏幕宽。
查看程序在何处轮询系统以获取显示设备边界:
GraphicsDevice[] deviceList = getLocalGraphicsEnvironment.getScreenDevices()
GraphicsConfiguration[] gc = deviceList[0].getConfigurations();
Rectangle deviceBounds = gc[0].getBounds();
Run Code Online (Sandbox Code Playgroud)
我看到它返回为 [ 0, 0, 2560, 1440 ],尽管 MacOS 系统报告显示为 [5120 x 2880 ]。
此外,作为应用程序输入的 MouseEvent 坐标被“减半”,以便与 deviceBounds 一致。
我找不到这些测试结果的任何解释。不用说,我很高兴该应用程序无需修改即可在 5K Retina iMac 上完美运行。
但是,我确实需要了解谁在自动缩小本机图形环境。MacOS 就是这么做的吗?Java16 对 Swing 应用程序做了什么?或者,Java16 MacOS JRE 为 Swing 应用程序做了什么?第四种可能性?
我看到这个 JEP ( http://openjdk.java.net/jeps/197 ) 引入了 3 种类型的代码缓存。
对我来说最明显的一个是-XX:NonNMethodCodeHeapSize。这是处理 JVM 内部数据的一个。
NonProfiledCodeHeapSize我不明白和之间有什么区别ProfiledCodeHeapSize。该文件说:
分层编译还引入了一种新的编译代码类型:插装编译代码(分析代码)。
我的理解是,这里的“仪表化”意味着“带有计数器”,那么假设这真的是C1编译代码的逻辑是什么?另一个是C2?
我有一个Set<Person>,我需要过滤内容,然后填充给定集中的另一组。
我对流还很陌生。
我能想到的第一个解决方案是链接stream().filter().foreach()
解决方案一:
private Set<Person> mapPerson(final Set<Student> student) {
final Set<Person> person = new HashSet<>();
student.stream()
.filter(student1 -> Objects.nonNull(student.getAge()))
.forEach(student1 -> person.add(Person.builder()
.id(student1.getId())
.status(student1.getStatus()))
.build()));
return person;
}
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否可以通过链接来完成stream().filter().map()?
将不胜感激任何帮助,如果可以两种方式进行,哪种方式是首选方式?
在Java的instanceof模式匹配中,使用附加标识符来缩小类型:
Object o = "test";
if (o instanceof String s) {
System.out.println(s.length()); // s is of type String
}
Run Code Online (Sandbox Code Playgroud)
为什么有必要?JEP似乎没有证明其合理性,但我确信这是有原因的。以下有什么不好的地方?
Object o = "test";
if (o instanceof String) {
System.out.println(o.length());
}
Run Code Online (Sandbox Code Playgroud)
TypeScript和Kotlin可以做类似的事情,而无需另一个标识符。
我猜这是一个向后兼容性的问题——Java 的怪癖通常是这样——但我想不出一个可以证明这一点的例子。
我唯一能想到的是,有人可能编写了类似于第二个示例预模式匹配的内容,并依赖它来不编译,但这似乎是一个薄弱的理由。
java ×10
java-16 ×10
java-11 ×3
java-record ×2
arity ×1
collections ×1
cryptography ×1
curve-25519 ×1
date ×1
java-stream ×1
java-time ×1
jit ×1
jvm ×1
netbeans ×1
netbeans-12 ×1
regex ×1
side-effects ×1
swing ×1