我一直在从事一个项目,需要我以这种方式解析字符串,我已经分离出了引发错误的小部分,
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
public class TestingYearMonth {
public static void main(String[] args) {
YearMonth yearMonth = YearMonth.parse("Feb-17", DateTimeFormatter.ofPattern("MMM-yy"));
System.out.println(yearMonth.getMonth() + " " + yearMonth.getYear());
}
}
Run Code Online (Sandbox Code Playgroud)
我的老师运行完全相同的代码,它返回输出没有问题。但是当我运行它时出现以下错误
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Feb-17' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.YearMonth.parse(YearMonth.java:295)
at com.ethanbradley.assignment6.TestingYearMonth.main(TestingYearMonth.java:10)
Run Code Online (Sandbox Code Playgroud)
我们检查了一下,我的 jdk 很好(jdk 11,亚马逊 coretto 版本),我真的不明白为什么这不起作用,请帮助哦明智的互联网人们......
在 Java 11 中,时钟系统使用毫秒精度,但显然在 Java 13 及更高版本中,它使用微秒精度,这导致我的测试失败。例如,OffsetDateTime.now()当我从数据库“2021-12-10T10:58:05.309595+01:00”读取此日期时,给我这个日期“2021-12-10T10:58:05.309594500+01:00”。我正在寻找一种方法,可以以它们应该相等的方式格式化第一个日期。我确实希望将其设置为 OffsetDateTime 类型而不是字符串。
更新:我意识到这个问题是在我将java版本从11升级到17时出现的,而不是在本地,当gitlab运行测试时我遇到了这个问题。
这是测试:
@Test
fun `can store, find and delete a failed-message`() {
// given: a failed-message
val failedMessage = FailedMessage(
failedMessageId = FailedMessageId("the-subcription", "the-message-id"),
messageAttributes = mapOf("one" to "een", "two" to "twee"),
messagePayload = "message-payload",
exception = "exception",
dateTime = OffsetDateTime.now(),
stackTrace = "stackey tracey"
)
failedMessageRepository.store(failedMessage)
assertEquals(failedMessage, failedMessageRepository.find(failedMessage.failedMessageId))
}
Run Code Online (Sandbox Code Playgroud)
由于日期时间不相等,该测试失败。这是日志:
<FailedMessage(failedMessageId=the-subcription-the-message-id, messageAttributes={one=een, two=twee}, messagePayload=message-payload, exception=exception, dateTime=2021-12-10T10:58:05.309594500+01:00, stackTrace=stackey tracey)>
but was:
<FailedMessage(failedMessageId=the-subcription-the-message-id, messageAttributes={one=een, two=twee}, messagePayload=message-payload, exception=exception, dateTime=2021-12-10T10:58:05.309595+01:00, stackTrace=stackey tracey)> …Run Code Online (Sandbox Code Playgroud) 我已将 Java EE 应用程序从 JDK 8 升级到 JDK 11。但是当我部署到 JBOSS EAP 7.3 服务器时,出现以下异常。
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: jdk.internal.ref.Cleaner from [Module "deployment.DFNNTPOMS_X_X_3.003.000.00.0.ear" from Service Module Loader]
at deployment.DFNNTPOMS_X_X_3.003.000.00.0.ear//net.openhft.chronicle.hash.impl.util.CleanerUtils.<clinit>(CleanerUtils.java:42)
... 63 more
Caused by: java.lang.ClassNotFoundException: jdk.internal.ref.Cleaner from [Module "deployment.DFNNTPOMS_X_X_3.003.000.00.0.ear" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:412)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:400)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at deployment.DFNNTPOMS_X_X_3.003.000.00.0.ear//net.openhft.chronicle.hash.impl.util.CleanerUtils.<clinit>(CleanerUtils.java:35)
... 63 more
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?
我有一个情况,我必须从字符串中替换一些字符(特殊、不可打印和其他特殊字符),如下所述
private static final String NON_ASCII_CHARACTERS = "[^\\x00-\\x7F]";
private static final String ASCII_CONTROL_CHARACTERS = "[\\p{Cntrl}&&[^\r\n\t]]";
private static final String NON_PRINTABLE_CHARACTERS = "\\p{C}";
stringValue.replaceAll(NON_ASCII_CHARACTERS, "").replaceAll(ASCII_CONTROL_CHARACTERS, "")
.replaceAll(NON_PRINTABLE_CHARACTERS, "");
Run Code Online (Sandbox Code Playgroud)
我们可以重构上面的代码意味着我们可以使用单个“replaceAll”方法并将所有条件放入其中吗?
有什么办法请指教。
由于wsimport自 JDK11 以来不再包含(在哪里下载以及如何安装 JAX-WS wsimport 工具?),我想我需要安装metro-jax-ws;但是,没有关于如何执行此操作的说明......
使用JDK 11.0.3。我有以下代码片段:
Set<String> allNumbersSet = customerInfoService.getCustomerPhoneNumbers(bankCustomerId);
additionalInformation
.map(info -> info.get(BANK_PE_CUSTOMER_ID_KEY))
.filter(StringUtils::isNotEmpty)
.ifPresent(id -> allNumbersSet.addAll(customerInfoService.getCustomerPhoneNumbers(id))); // fails here
Run Code Online (Sandbox Code Playgroud)
获取电话号码的地方就是Collectors.toSet():
@Override
public Set<String> getCustomerPhoneNumbers(String customerId) {
return backOfficeInfoClient.getCustByHashNo(customerId).getPropertyLOVs()
.flatMap(property -> property.getValues().values().stream())
.collect(Collectors.toSet());
}
Run Code Online (Sandbox Code Playgroud)
但是,它失败了:
java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.addAll(ImmutableCollections.java:76)
at service.impl.UserManagementServiceImpl.lambda$validateNewLogin$3(UserManagementServiceImpl.java:69)
Run Code Online (Sandbox Code Playgroud)
如果我像下面这样更新:
var allNumbersSet = new HashSet<>(customerInfoService.getCustomerPhoneNumbers(bankCustomerId));
Run Code Online (Sandbox Code Playgroud)
现在效果很好。
上面的代码使用有什么问题吗?您能解释一下为什么会出现这种情况吗?
此方法调用由调用 Hazelcast 缓存包围 - 之前和之后。正如评论中提到的,这可能是这种行为的原因:
缓存的值使用不可变集合表示,这是有道理的,因为这允许共享而无需防御性副本
解决方案:
找到了如何重写此逻辑并在不合并两组的情况下执行该操作的方法:
var numbersSet = customerInfoService.getCustomerPhoneNumbers(id);
if (!numbersSet.contains(newLogin)) {
var peNumbersSet = additionalInformation
.map(info -> info.get(BANK_PE_CUSTOMER_ID_KEY))
.filter(StringUtils::isNotEmpty)
.map(customerInfoService::getCustomerPhoneNumbers)
.orElseGet(Collections::emptySet);
if (!peNumbersSet.contains(newLogin)) {
throw …Run Code Online (Sandbox Code Playgroud) 如果我没记错的话
javac filename.java-> 编译并生成classname.class(es)java classname没有 .class 扩展名但是当我尝试java filename.java成功执行而java classname命令给出以下错误时,
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
Run Code Online (Sandbox Code Playgroud)
java版本
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
Run Code Online (Sandbox Code Playgroud)
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}
Run Code Online (Sandbox Code Playgroud)
javap HelloWorld.class给出以下输出
Compiled from "HelloWorld.java"
public …Run Code Online (Sandbox Code Playgroud) 为什么在包含依赖项时会出现此错误?
\nCould not find implementation class: org.janusgraph.diskstorage.cql.CQLStoreManager\nRun Code Online (Sandbox Code Playgroud)\nCould not find implementation class: org.janusgraph.diskstorage.cql.CQLStoreManager\nRun Code Online (Sandbox Code Playgroud)\n请注意,我对 JanusGraph 和 GraphDB 以及 Cassandra 都是新手。
\ndocker run --name jg-cassandra -d -e CASSANDRA_START_RPC=true -p 9160:9160 -p 9042:9042 -p 7199:7199 -p 7001:7001 -p 7000:7000 cassandra:3.11janusgraph.cql.properties到 Cassandra [Docker 容器]\nmaven-package cassandra-3.0 docker-container janusgraph java-11
这不是重复的问题。我的问题是我想将我的JDK从8更改为另一个版本(11或12),但是出现此错误:
“所选目录不是jdk的有效主目录”
我的JDk地址是:C:\ Program Files \ Java \ jdk-11.0.2
IntelliJ版本:2017.2.5
我搜索了大约3个小时,但问题仍未解决。
I'm trying to set up a service which listens to a RabbitMQ server and I've set up code using the RabbitMQ Sample code from Github, which includes the following try-with-resources block
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
When I use the same code and build and run this service using java -cp myJar.jar MyService, it just starts and ends immediately (and echo $? returns 0)
However, if I replace the …