简体   繁体   中英

How to set the Java version in JShell?

I'm using JShell from JDK 11 to experiment some behaviors with generics.

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 .

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 -C "-source 11"

... But it won't work. I tried it just now, and the Jshell fails to parse this with an exception. It should not be the case, so I looked into the code of the Arguments class that throws the error. 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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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