问题
I'm trying to use JShell provided by JDK11 to run some simple command. But when I type:
jshell>System.out.println("Hello World!");
It gives me the error:
Exception in thread "main" java.lang.NullPointerException: charsetName
at java.base/java.lang.String.<init>(String.java:464)
at java.base/java.lang.String.<init>(String.java:537)
at jdk.internal.le/jdk.internal.jline.extra.AnsiInterpretingOutputStream.write(AnsiInterpretingOutputStream.java:92)
at java.base/java.io.OutputStream.write(OutputStream.java:157)
at java.base/sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:233)
at java.base/sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:312)
at java.base/sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:316)
at java.base/sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:153)
at java.base/java.io.OutputStreamWriter.flush(OutputStreamWriter.java:254)
at jdk.internal.le/jdk.internal.jline.console.ConsoleReader.flush(ConsoleReader.java:1052)
at jdk.internal.le/jdk.internal.jline.console.ConsoleReader.accept(ConsoleReader.java:2029)
at jdk.internal.le/jdk.internal.jline.console.ConsoleReader.readLine(ConsoleReader.java:2756)
at jdk.internal.le/jdk.internal.jline.console.ConsoleReader.readLine(ConsoleReader.java:2383)
at jdk.internal.le/jdk.internal.jline.console.ConsoleReader.readLine(ConsoleReader.java:2371)
at jdk.jshell/jdk.internal.jshell.tool.ConsoleIOContext.readLine(ConsoleIOContext.java:142)
at jdk.jshell/jdk.internal.jshell.tool.JShellTool.getInput(JShellTool.java:1261)
at jdk.jshell/jdk.internal.jshell.tool.JShellTool.run(JShellTool.java:1174)
at jdk.jshell/jdk.internal.jshell.tool.JShellTool.start(JShellTool.java:975)
at jdk.jshell/jdk.internal.jshell.tool.JShellToolBuilder.start(JShellToolBuilder.java:254)
at jdk.jshell/jdk.internal.jshell.tool.JShellToolProvider.main(JShellToolProvider.java:120)
then end the JShell program.
My java version is as below:
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
回答1:
It turns out that the printing of the active code page when spawning a new CMD is the culprit. For example, try to run cmd.exe
and see if you get a similar output:
Microsoft Windows [Version 10.0.17763.253]
(c) 2018 Microsoft Corporation. All rights reserved.
Active code page: 65001
This crashes after writing a line of code like an import statement or the above case of printing hello world. JShell apparently expected something else.
The additional output of the active code page seems to throw off a lot of other parsers too. I actually came across the issue via a different exception, which happened while running mvn release
. Turns out that Maven's release plugin heavily shells out to cmd.exe
and tries to parse the output. That yields funny exceptions like this:
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 6: Active%20code%20page:%2065001
C:/Users/USER_NAME/git/SOME_PROJECT
at java.net.URI$Parser.fail (URI.java:2915)
at java.net.URI$Parser.checkChars (URI.java:3086)
at java.net.URI$Parser.parse (URI.java:3112)
at java.net.URI.<init> (URI.java:600)
at java.net.URI.create (URI.java:881)
at org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer.resolveURI (GitStatusConsumer.java:249)
Solution
I had to disable the AutoRun
via regedit
that ran the chcp 65001
command.
This can be found in either of the following paths:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
The following SO answer will give more information about that and is the ultimate answer to this question: https://stackoverflow.com/a/48203959/540873
Note that this might also happen with any other command in AutoRun
that will ECHO
something, in my case it just was the chcp
command.
Java version for reference:
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
来源:https://stackoverflow.com/questions/53900918/jshell-error-java-lang-nullpointerexception-charsetname