3

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)
Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
ZhengguanLi
  • 113
  • 1
  • 2
  • 8
  • 4
    Did you modify any of the startup configurations to start `jshell`? It's not reproducible unless you state what steps you'd followed. Would be good to update the question with exact version of JDK used as well. – Naman Dec 23 '18 at 03:11
  • I don' think so, I just found what jshell is and tried my first time to play with it. – ZhengguanLi Dec 23 '18 at 21:22
  • Not sure if someone here can help, unless able to reproduce the same or without further details over the exception. – Naman Dec 24 '18 at 01:40
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Dec 24 '18 at 02:45

1 Answers1

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)
Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
  • 1
    same issue with OpenJDK openjdk version "11.0.2" 2019-01-15 on windows PowerShell with chcp 65001 (same issues with chcp 28591 (iso-8859-1), 65000 (utf-7), 20127 (us-ascii) . Setting chcp 1252 or 850 solved the issue. Well, partially as output is incorrect: `System.out.println("é")` emits `Ú`. Thanks for identifying this. – jgran Mar 29 '19 at 16:49
  • 1
    the printing itself is not the culprit, but it seems to have an issue with 65001 – Leonard Brünings Apr 24 '19 at 15:24
  • yeah you're right, in the shell case it is the main problem. I had the issue with maven in the first place which definitely relied on the forked process output. Sorry for conflating the two issues here. – Thomas Jungblut Apr 24 '19 at 15:59