简体   繁体   English

如何在 JShell 中设置 Java 版本?

[英]How to set the Java version in JShell?

I'm using JShell from JDK 11 to experiment some behaviors with generics.我正在使用JDK 11 中的 JShell来试验一些具有泛型的行为。

I would like to set the interpreted java version to version 8. This would allow me to have the same type inference behaviors as defined in the Java Language Specification 8 .我想将解释的 java 版本设置为版本 8。这将允许我具有与Java Language Specification 8 中定义的相同的类型推断行为。

How can I do it?我该怎么做?

The flag you would like to apply is:您要申请的标志是:

"-source 8"

The way to pass this onto the compiler behind JShell would be:将其传递给 JShell 后面的编译器的方法是:

"jshell -C "-source 11"

... But it won't work. ......但它不会工作。 I tried it just now, and the Jshell fails to parse this with an exception.我刚刚试过了,Jshell 无法解析这个异常。 It should not be the case, so I looked into the code of the Arguments class that throws the error.不应该是这样,所以我查看了抛出错误的 Arguments 类的代码。 It turned out that javac options are grouped into different categories, and only the BASIC commands are allowed via JShell (com.sun.tools.javac.main.Option.java:1308).结果证明, javac选项被分为不同的类别,并且只允许通过 JShell (com.sun.tools.javac.main.Option.java:1308)使用BASIC命令

Setting the source flag does not belong to this group, so the validator throws it out and the setting will not be applied.设置源标志不属于该组,因此验证器将其丢弃并且不会应用该设置。

If you want to verify this, you can try to pass a BASIC type of param to the javac like "-p", and you will see it correctly passed.如果您想验证这一点,您可以尝试将 BASIC 类型的参数传递给 javac,例如“-p”,您将看到它正确传递。

这是不可能的,JShell 工具使用 Java 平台模块系统 (JPMS),这是一个 Java 9 功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM