Search code examples
javawindowsbackspacejshell

Jshell crashes when i press BackSpace button in windows cmd


when I open windows cmd and type jshell , it works, but when I'm writing something . whenever I press the BackSpace key . for example if I want to delete a letter that is wrong . jshell crashes with bellow error , i'm using windows 10 , and C:\Program Files\Java\jdk-11.0.1 is my only path in windows System Environment variables , i also use cmd as terminal . here is 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.readLine(ConsoleReader.java:3259)
        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)

Solution

  • This is an issue with the active code page. Specifically 65001 is a problem and popular one to have so my best guess is that this is the one being used but see the link later for some choices.

    On windows:

    > java --version
    java 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)
    

    Check the active code page with chcp. If it is set to 65001 (maybe you've been playing around with this hack to get utf-8 characters to display in your terminal) the issue is reliably reproducible.

    > chcp 65001
    Active code page: 65001
    > jshell
    |  Welcome to JShell -- Version 11.0.1
    |  For an introduction type: /help intro
    
    jshell>TypeAnything<backspace>Exception in thread "main" java.lang.NullPointerException: charsetName
            at java.base/java.lang.String.<init>(String.java:464)
            at ...
    
    

    The solution

    Take your pick from https://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html, but chcp 850 should do the trick.

    > chcp 850 && jshell
    Active code page: 850
    |  Welcome to JShell -- Version 11.0.1
    |  For an introduction type: /help intro
    
    jshell> TypeAnything<backspace>
    

    PowerShell specific

    The idea is the same (change the console encoding) but the commands are slightly different. Again the idea is to change the encoding. Look at the current code page with [Console]::OutputEncoding.CodePage, we want to switch that to one from the list like 850.

    > [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(850)
    > jshell