Skip to content

Commit e7e3712

Browse files
committed
8300010: UnsatisfiedLinkError on calling System.console().readPassword() on Windows
Reviewed-by: alanb
1 parent 0b9ff06 commit e7e3712

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

src/java.base/windows/native/libjava/Console_md.c

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,22 +31,22 @@
3131
#include <stdlib.h>
3232
#include <Wincon.h>
3333

34-
static HANDLE hStdOut = INVALID_HANDLE_VALUE;
35-
static HANDLE hStdIn = INVALID_HANDLE_VALUE;
3634
JNIEXPORT jboolean JNICALL
3735
Java_java_io_Console_istty(JNIEnv *env, jclass cls)
3836
{
39-
if (hStdIn == INVALID_HANDLE_VALUE &&
40-
(hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
41-
return JNI_FALSE;
42-
}
43-
if (hStdOut == INVALID_HANDLE_VALUE &&
44-
(hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
37+
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
38+
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
39+
40+
if (hStdIn == INVALID_HANDLE_VALUE ||
41+
hStdOut == INVALID_HANDLE_VALUE) {
4542
return JNI_FALSE;
4643
}
44+
4745
if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
48-
GetFileType(hStdOut) != FILE_TYPE_CHAR)
46+
GetFileType(hStdOut) != FILE_TYPE_CHAR) {
4947
return JNI_FALSE;
48+
}
49+
5050
return JNI_TRUE;
5151
}
5252

@@ -63,24 +63,3 @@ Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
6363
sprintf(buf, "cp%d", cp);
6464
return JNU_NewStringPlatform(env, buf);
6565
}
66-
67-
JNIEXPORT jboolean JNICALL
68-
Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)
69-
{
70-
DWORD fdwMode;
71-
jboolean old;
72-
if (! GetConsoleMode(hStdIn, &fdwMode)) {
73-
JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
74-
return !on;
75-
}
76-
old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
77-
if (on) {
78-
fdwMode |= ENABLE_ECHO_INPUT;
79-
} else {
80-
fdwMode &= ~ENABLE_ECHO_INPUT;
81-
}
82-
if (! SetConsoleMode(hStdIn, fdwMode)) {
83-
JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");
84-
}
85-
return old;
86-
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
#include "jni.h"
27+
#include "jni_util.h"
28+
#include "jvm.h"
29+
#include "jdk_internal_io_JdkConsoleImpl.h"
30+
31+
#include <stdlib.h>
32+
#include <Wincon.h>
33+
34+
JNIEXPORT jboolean JNICALL
35+
Java_jdk_internal_io_JdkConsoleImpl_echo(JNIEnv *env, jclass cls, jboolean on)
36+
{
37+
DWORD fdwMode;
38+
jboolean old;
39+
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
40+
41+
if (! GetConsoleMode(hStdIn, &fdwMode)) {
42+
JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
43+
return !on;
44+
}
45+
old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
46+
if (on) {
47+
fdwMode |= ENABLE_ECHO_INPUT;
48+
} else {
49+
fdwMode &= ~ENABLE_ECHO_INPUT;
50+
}
51+
if (! SetConsoleMode(hStdIn, fdwMode)) {
52+
JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");
53+
}
54+
return old;
55+
}

0 commit comments

Comments
 (0)