标签: java-10

为什么数组[idx ++] + ="a"在Java 8中增加一次idx,在Java 9和10中增加两次?

对于挑战,一位代码高尔夫球手 编写了以下代码:

import java.util.*;
public class Main {
  public static void main(String[] args) {
    int size = 3;
    String[] array = new String[size];
    Arrays.fill(array, "");
    for(int i = 0; i <= 100; ) {
      array[i++%size] += i + " ";
    }
    for(String element: array) {
      System.out.println(element);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在Java 8中运行此代码时,我们得到以下结果:

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 …
Run Code Online (Sandbox Code Playgroud)

java javac java-8 java-9 java-10

730
推荐指数
1
解决办法
7万
查看次数

如何在Java 9中解决java.lang.NoClassDefFoundError:javax/xml/bind/JAXBException

我有一些代码使用JAXB API类,它们是作为Java 6/7/8中JDK的一部分提供的.当我使用Java 9运行相同的代码时,在运行时我得到错误,指示无法找到JAXB类.

自Java 6以来,JAXB类已作为JDK的一部分提供,为什么Java 9不再能够找到这些类?

java jaxb java-9 java-10 java-11

718
推荐指数
27
解决办法
46万
查看次数

无法使用Maven编译简单的Java 10/Java 11项目

我有一个简单的Maven项目:

src
??? main
    ??? java
        ??? module-info.java
pom.xml
Run Code Online (Sandbox Code Playgroud)

pom.xml中:

<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>example</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <release>10</release>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

当我通过构建项目时mvn -X install -DskipTests=true,它失败了:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project example: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile failed.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) …
Run Code Online (Sandbox Code Playgroud)

java maven java-10 module-info java-11

111
推荐指数
6
解决办法
8万
查看次数

如何在Ubuntu下安装JDK 10?

如何在Ubuntu上安装Java Development Kit(JDK)10?

Oracle帮助中心的安装说明仅说明如何在Linux平台上下载和解压缩归档,无需任何系统设置.

java ubuntu java-10

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

为什么不能为Java中的var关键字分配lambda表达式?

允许var在Java 10中分配如下字符串:

var foo = "boo";
Run Code Online (Sandbox Code Playgroud)

虽然不允许为其分配lambda表达式,例如:

var predicateVar = apple -> apple.getColor().equals("red");
Run Code Online (Sandbox Code Playgroud)

为什么不能推断的λ或方法引用类型时,它可以推断出休息等String,ArrayList,用户类等?

java lambda java-10

62
推荐指数
5
解决办法
3782
查看次数

JDK8和JDK10上三元运算符的行为差异

请考虑以下代码

public class JDK10Test {
    public static void main(String[] args) {
        Double d = false ? 1.0 : new HashMap<String, Double>().get("1");
        System.out.println(d);
    }
}
Run Code Online (Sandbox Code Playgroud)

在JDK8上运行时,此代码会打印,null而在JDK10上会生成此代码NullPointerException

Exception in thread "main" java.lang.NullPointerException
    at JDK10Test.main(JDK10Test.java:5)
Run Code Online (Sandbox Code Playgroud)

除了JDK10编译器生成的两个与自动装箱相关的附加指令外,编译器生成的字节码几乎完全相同,并且似乎负责NPE.

15: invokevirtual #7                  // Method java/lang/Double.doubleValue:()D
18: invokestatic  #8                  // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
Run Code Online (Sandbox Code Playgroud)

这种行为是JDK10中的错误还是故意更改以使行为更严格?

JDK8:  java version "1.8.0_172"
JDK10: java version "10.0.1" 2018-04-17
Run Code Online (Sandbox Code Playgroud)

java unboxing javac java-8 java-10

57
推荐指数
2
解决办法
3514
查看次数

当module-info.java存在时,findResource("")返回null,为什么会这样?

我正在调试为什么在module-info.java我的Spring Boot应用程序中,spring-orm在启动时抛出异常.这是例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:na]
    at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.8.RELEASE.jar:na]
    at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.8.RELEASE.jar:na]
    at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.8.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:na]
    at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:na]
    at tech.flexpoint.dashmanserver/tech.flexpoint.dashmanserver.DashmanServerApplication.main(DashmanServerApplication.java:13) [classes/:na] …
Run Code Online (Sandbox Code Playgroud)

java java-platform-module-system spring-boot java-10 java-module

51
推荐指数
1
解决办法
1264
查看次数

具有德语语言环境的SimpleDateFormat - Java 8与Java 10+

我在遗留应用程序中有代码和测试用例,可归纳如下:

@Test
public void testParseDate() throws ParseException {
    String toParse = "Mo Aug 18 11:25:26 MESZ +0200 2014";
    String pattern = "EEE MMM dd HH:mm:ss z Z yyyy";

    DateFormat dateFormatter = new SimpleDateFormat(pattern, Locale.GERMANY);
    Date date = dateFormatter.parse(toParse);

    //skipped assumptions
}
Run Code Online (Sandbox Code Playgroud)

该测试通过Java 8及以下版本.然而,随着Java 10向上,这导致了一个java.text.ParseException: Unparseable date: "Mo Aug 18 11:25:26 MESZ +0200 2014".

备案:另外de_DE,异常也抛出了语言环境 de_CH,de_AT,de_LU.

我知道使用JDK 9(JEP 252)更改了日期格式.但是,我认为这是破坏向后兼容性的颠覆性变化.摘录:

在JDK 9中,Unicode Consortium的公共区域设置数据存储库(CLDR)数据作为默认区域设置数据启用,因此您可以使用标准区域设置数据而无需任何进一步操作.

在JDK …

java simpledateformat java-8 java-9 java-10

49
推荐指数
1
解决办法
2700
查看次数

正确修复Java 10抱怨jaxb-impl 2.3.0的非法反射访问?

我们正在考虑将一些遗留代码升级到Java 10.由于JAXB默认不可见(编辑:正确的长期解决方案不是使用各种JVM标志来规避症状,而是正确修复它)我已将此片段添加到我的pom.xml:

    <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

不幸的是,在stderr启动时仍然会出现警告.显然这不是正确的解决方法.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/home/tra/.m2/repository/com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
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)

完整的输出--illegal-access=debug是:

WARNING: Illegal …
Run Code Online (Sandbox Code Playgroud)

java jaxb maven java-10

36
推荐指数
3
解决办法
8850
查看次数

Java 10:Java 7的Diamond推理是否适用于本地类型推断?

JEP 286开始,我们看到我们将能够var在JDK 10(18.3)中使用本地类型推理().JEP表示以下编译,这是预期的:

var list = new ArrayList<String>();  // infers ArrayList<String>
Run Code Online (Sandbox Code Playgroud)

我很想知道如果我们尝试以下内容会发生什么:

var list = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

我在第二个片段中提出的建议是否会编译?如果是这样(我怀疑),ArrayList接受是否Object为通用类型?

我自己尝试一下,但我无法访问任何我可以安装早期版本的机器.

谢谢!

java type-inference diamond-operator java-7 java-10

35
推荐指数
3
解决办法
1636
查看次数