As per the javadoc of ByteArrayOutputStream
's toString(Charset)
method, it was added in Java10, and is thus not available on JDK8.
sourceCompatibility = 1.8
targetCompatibility = 1.8
so it can run with java 8.
That's not what that means. That merely means the source files are compiled using the JDK8 idea of java-the-language, and that the produces class files are in JDK8 format. It does nothing to guarantee that you are only using the JDK8 idea of the core libraries. Those gradle options are the gradle equivalent of javac
's --source
resp. --target
options.
Those are the wrong options.
With javac, there is now a --release
option (introduced in.. I think JDK8?) which covers both source and target compatibility, and even warns about trying to use core library calls that weren't in the stated release. Let's try it, with JDK14 which I happen to have installed on my machine (but should work with JDK11 too):
echo "class Test {{ new java.io.ByteArrayOutputStream().toString(java.nio.charset.StandardCharsets.UTF_8); }}" > Test.java
javac -version
> javac 14.0.1
javac --release 8 Test.java
> Test.java:1: error: no suitable method found for toString(Charset)
javac --release 11 Test.java
> [no message; it compiles fine]
As per the current gradle docs, sourceCompatibility
and targetCompatibility
are deprecated and should no longer be used. Use options.release = 8
instead, which is the gradle variant of the --release
option. Yay!
NB: .toString("UTF-8")
is ugly, but does work; it was introduced before java8.
NB2:
On my computer, it works perfectly fine with java 8 and java 15.
Nope. You weren't running it on java8 on your machine. If you had, the app would have failed with the same error. Must have accidentally used the wrong one :P
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…