Unable to create JNA/JANSI terminal on Windows: [6] Handle not valid

388 views
Skip to first unread message

Raffaele Sgarro

unread,
Mar 2, 2023, 4:13:23 AM3/2/23
to jline-users
Hi, I want to get the keys pressed by a user as soon as they are typed, without waiting for ENTER. I am on Windows 11 and launch my app with Java 11 in Intellij; I added the latest jline-terminal and jline-terminal-jna to my project, but as soon as I build my terminal with:

jline3 = TerminalBuilder.builder()
.system(true)
.jna(true)
.jansi(false)
.exec(false)
.dumb(false)
.build();

I get the error:

java.lang.IllegalStateException: Unable to create a terminal
at org.jline.terminal.TerminalBuilder.doBuild(TerminalBuilder.java:363) ~[jline-terminal-3.22.0.jar:na]
at org.jline.terminal.TerminalBuilder.build(TerminalBuilder.java:303) ~[jline-terminal-3.22.0.jar:na]

Digging into the code, it seems that inside JnaWinSysTerminal#isWindowsSystemStream the native method Kernel32.INSTANCE.GetConsoleMode always throws error "[6] Handle not valid" for Input, Output and Error streams.

Can you help me?

Guillaume Nodet

unread,
Mar 2, 2023, 4:18:06 AM3/2/23
to jline...@googlegroups.com
You should try to launch in a real terminal first.  When using system(true).dumb(false), the terminal will be forced to use the system terminal, which may not be available from the IDE.
Please also report the output of the following command in your terminal to help diagnosing the problem 
  java -cp jline-3.22.0.jar;jna-5.13.0.jar  org.jline.terminal.impl.Diag  

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jline-users/e33f73ae-2d72-4e64-890d-d589ec586b34n%40googlegroups.com.

Raffaele Sgarro

unread,
Mar 2, 2023, 4:49:11 AM3/2/23
to jline-users
I tried from the Windows terminal and got the same error. This is the diagnostics you asked for:

System properties
=================
os.name =         Windows 11
OSTYPE =          null
MSYSTEM =         null
PWD =             null
ConEmuPID =       null
WSL_DISTRO_NAME = null
WSL_INTEROP =     null

OSUtils
=================
IS_WINDOWS = true
IS_CYGWIN =  false
IS_MSYSTEM = false
IS_WSL =     false
IS_WSL1 =    false
IS_WSL2 =    false
IS_CONEMU =  false
IS_OSX =     false

JnaSupport
=================
StdIn stream =    false
StdOut stream =   false
StdErr stream =   false
StdIn stream name =     null
StdOut stream name =    null
StdErr stream name =    null
Unable to open terminal: com.sun.jna.LastErrorException: [6] Handle non valido.

JansiSupport
=================
Jansi support not available: java.io.IOException: Unable to find terminal provider jansi

Exec Support
=================
StdIn stream =    false
StdOut stream =   false
StdErr stream =   false
StdIn stream name =     null
StdOut stream name =    null
StdErr stream name =    null
Not supported by provider
com.sun.jna.LastErrorException: [6] Handle non valido.
at com.sun.jna.Native.invokeVoid(Native Method)
at com.sun.jna.Function.invoke(Function.java:415)
at com.sun.jna.Function.invoke(Function.java:361)
at com.sun.jna.Library$Handler.invoke(Library.java:270)
at org.jline.terminal.impl.jna.win.$Proxy0.GetConsoleMode(Unknown Source)
at org.jline.terminal.impl.jna.win.JnaWinSysTerminal.createTerminal(JnaWinSysTerminal.java:53)
at org.jline.terminal.impl.jna.JnaTerminalProvider.winSysTerminal(JnaTerminalProvider.java:55)
at org.jline.terminal.impl.jna.JnaTerminalProvider.sysTerminal(JnaTerminalProvider.java:46)
at org.jline.terminal.impl.Diag.testProvider(Diag.java:99)
at org.jline.terminal.impl.Diag.diag(Diag.java:57)
at org.jline.terminal.impl.Diag.main(Diag.java:26)
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jline.terminal.impl.exec.ExecTerminalProvider (file:/C:/Users/Raffaele/.gradle/caches/modules-2/files-2.1/org.jline/jline-terminal/3.22.0/7989e58c2fe3e6daa64eb6031c1a9ca7db2b243e/jline-terminal-3.22.0.jar) to constructor java.lang.ProcessBuilder$RedirectPipeImpl()
WARNING: Please consider reporting this to the maintainers of org.jline.terminal.impl.exec.ExecTerminalProvider
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Guillaume Nodet

unread,
Mar 2, 2023, 5:10:58 AM3/2/23
to jline...@googlegroups.com
Not sure what happens exactly.  I've tried on a clean Windows 11 and it works correctly.
I would advise to debug and see if the what the value for the console handles are in
However, I have no idea why that could be wrong.  Are you using a common JDK ?

You could also try to add jansi to the classpath and see if it gives similar results.

Raffaele Sgarro

unread,
Mar 2, 2023, 6:58:59 AM3/2/23
to jline...@googlegroups.com
What are the "expected" values? It seems they always change, like a file descriptor.

Anyway, what I'm trying to accomplish is a single line text editor that handles keyboard input to print Unicode emojis. I just need to enter raw mode, read from the keyboard, overwrite what's currently on the last line and finally return to line mode and exit.

Can you point me to a similar program in your sources and tell me how it's expected to be run, if any?

Actually I cannot find a method to switch back from raw to line mode.

You received this message because you are subscribed to a topic in the Google Groups "jline-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jline-users/wBBb9zky3gQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jline-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jline-users/CAEQV%2BEGYj3vxVdU9o4GLHJvMOZqEcjeNiZx-Yejcdy%3DOTRq2jA%40mail.gmail.com.

Guillaume Nodet

unread,
Mar 2, 2023, 7:31:18 AM3/2/23
to jline...@googlegroups.com
On Thu, Mar 2, 2023 at 12:59 PM Raffaele Sgarro <raffael...@gmail.com> wrote:
What are the "expected" values? It seems they always change, like a file descriptor.

Yes, if they are not equals to -1 or 0, it means they should be valid values.  Weird...
Can you give jansi a try ?
 

Anyway, what I'm trying to accomplish is a single line text editor that handles keyboard input to print Unicode emojis. I just need to enter raw mode, read from the keyboard, overwrite what's currently on the last line and finally return to line mode and exit.

Can you point me to a similar program in your sources and tell me how it's expected to be run, if any?

I don't see unfortunately.
 
Actually I cannot find a method to switch back from raw to line mode.

Attributes attributes = terminal.enterRawMode();

try { 

    // do in raw mode

} finally {

    terminal.setAttributes(attributes);

}



Raffaele Sgarro

unread,
Mar 10, 2023, 6:48:38 AM3/10/23
to jline...@googlegroups.com
Thanks a lot! What if I contribute such an application to the demos of jline3? It would be easier for me to get your support and it could benefit other users. Maybe I can fork a "demo/emoji-line-editor" branch form master and work there.

The application is simply a text editor that replaces ":-)" with "🙂" as the user types.

BTW, what's the API for setting the cursor position and making it a blinking block?

Guillaume Nodet

unread,
Mar 10, 2023, 6:55:37 AM3/10/23
to jline...@googlegroups.com
On Fri, Mar 10, 2023 at 12:48 PM Raffaele Sgarro <raffael...@gmail.com> wrote:
Thanks a lot! What if I contribute such an application to the demos of jline3? It would be easier for me to get your support and it could benefit other users. Maybe I can fork a "demo/emoji-line-editor" branch form master and work there.

Sure, good idea.


The application is simply a text editor that replaces ":-)" with "🙂" as the user types.

BTW, what's the API for setting the cursor position and making it a blinking block?

There may be an escape sequence, but it's not really standard afaik.
Basically, what is supported is:
  * style using https://github.com/jline/jline3/blob/master/terminal/src/main/java/org/jline/utils/AttributedStyle.java (which includes blinking, but for text, not cursor)

Guillaume
 
Reply all
Reply to author
Forward
0 new messages