javaintellij-ideajlinejline3jansi

JLine3 "Unable to create a system terminal" after build


I just tried to implement JLine with Jansi, but it throws always the same UnsupportedOperationException referring to org.jline.terminal.impl.jansi.JansiSupportImpl#winSysTerminal. Inspecting this method there is only one simple if which decides on this exception.

Fernflower decompiled of given source

So the version numbers must be the problem... to check them I copied the code which detects the versions to my source

public static void main(String[] args) throws Exception {

    String v = Ansi.class.getPackage().getImplementationVersion();
    System.out.println(Ansi.class.getPackage());
    if (v != null) {
        Matcher m = Pattern.compile("([0-9]+)\\.([0-9]+)([\\.-]\\S+)?").matcher(v);
        if (m.matches()) {
            System.out.println("Major: " + Integer.parseInt(m.group(1)));
            System.out.println("Minor: " + Integer.parseInt(m.group(2)));
        }
    }

    //System.setProperty("jansi.passthrough", "true");
    AnsiConsole.systemInstall();

    String prompt = Ansi.ansi()
            .eraseScreen()
            .fg(Ansi.Color.BLUE).bold().a("Console")
            .fgBright(Ansi.Color.BLACK).bold().a(" > ")
            .reset().toString();

    Terminal terminal = TerminalBuilder.builder()
            .system(true)
            .dumb(false)
            .encoding(Charset.forName("UTF-8"))
            .name("Terminal")
            .jna(false)
            .jansi(true)
            .build();

    LineReader reader = LineReaderBuilder.builder()
            .terminal(terminal)
            .build();

    String line;

    while ((line = reader.readLine(prompt)) != null) {
        System.out.println(line);
    }

}

The outcome surprised me: While it returns package org.fusesource.jansi, jansi, version 1.17.1, Major: 1, Minor: 17 in IntelliJ, but the console does not work because intellijs virtual console, it returns only package org.fusesource.jansi when I run it from command line after building. This must be caused by intellij removing the manifests, that contain the information. They cannot be packed because they got the same name as the manifest I need to run the jar file.

Do you guys know any solution or workaround? How do you handle with this exception? Or is there anything I failed?


Solution

  • It seems you're embedding Jansi in your own jar. If you, you should be able to hack the MANIFEST.MF of you jar to include the relevant information for Jansi.

    Manifest-Version: 1.0
    Created-By: peter
    ...
    
    Name: org/fusesource/jansi/
    Implementation-Version: 1.17.1