code

System.console ()이 null을 반환합니다.

codestyles 2020. 10. 17. 10:32
반응형

System.console ()이 null을 반환합니다.


내가 사용하고 있었다 readLineBufferedReader입력 / 사용자의 새 암호를 얻을 수 있지만, 내가 사용하려고두면 암호 마스크 싶어 java.io.Console클래스를. 문제는 애플리케이션이 Eclipse에서 디버깅 될 때 System.console()반환 된다는 것 null입니다. 저는 Java를 처음 사용하고 Eclipse가 이것이 최선의 방법인지 확실하지 않습니까? 소스 파일을 마우스 오른쪽 버튼으로 클릭하고 "Debug As"> "Java Application"을 선택합니다. 해결 방법이 있습니까?


이것은 eclipse 버그 # 122429 입니다.


이 코드 스 니펫은 트릭을 수행해야합니다.

private String readLine(String format, Object... args) throws IOException {
    if (System.console() != null) {
        return System.console().readLine(format, args);
    }
    System.out.print(String.format(format, args));
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    return reader.readLine();
}

private char[] readPassword(String format, Object... args)
        throws IOException {
    if (System.console() != null)
        return System.console().readPassword(format, args);
    return this.readLine(format, args).toCharArray();
}

Eclipse에서 테스트하는 동안 비밀번호 입력이 명확하게 표시됩니다. 적어도 테스트 할 수 있습니다. 테스트하는 동안 실제 암호를 입력하지 마십시오. 프로덕션 용도로 보관하십시오.).


System.console() 콘솔이 없으면 null을 반환합니다.

코드에 간접 계층을 추가 하거나 외부 콘솔에서 코드를 실행하고 원격 디버거를 연결 하여이 문제를 해결할 수 있습니다 .


또한 간단한 명령 줄 응용 프로그램을 작성하려고 할 때이 문제가 발생했습니다.

System.in에서 고유 한 BufferedReader 객체를 만드는 또 다른 대안은 다음과 같이 java.util.Scanner를 사용하는 것입니다.

import java.util.Scanner;

Scanner in;
in = new Scanner(System.in);

String s = in.nextLine();

물론 이것은 콘솔에 대한 드롭 인 대체가 아니지만 다양한 입력 기능에 대한 액세스를 제공합니다.

다음 은 Oracle의 Scanner에 대한 더 많은 문서입니다 .


문서 에 따르면 :

가상 머신이 예를 들어 백그라운드 작업 스케줄러에 의해 자동으로 시작되면 일반적으로 콘솔이 없습니다.


API 에 따르면 :

" 표준 입력 및 출력 스트림을 리디렉션하지 않고 대화 형 명령 줄에서 가상 머신을 시작하면 해당 콘솔이 존재하며 일반적으로 가상 머신이 시작된 키보드 및 디스플레이에 연결됩니다. 가상 머신이 시작된 경우 예를 들어 백그라운드 작업 스케줄러에 의해 자동으로 발생하면 일반적으로 콘솔이 없습니다. "


Netbeans에서 응용 프로그램을 실행할 때이 오류 메시지가 나타납니다. 다른 답변으로 판단하면 IDE에서 응용 프로그램을 실행할 때 이런 일이 발생하는 것 같습니다. 이 질문을 살펴보면 Java의 콘솔에서 읽으려고 시도하는 것은

대부분의 IDE는 Java 코드를 실행하기 위해 java.exe 대신 javaw.exe를 사용합니다.

The solution is to use the command line/terminal to get the Console.


I believe that in the run configurations for Eclipse, you can configure whether to assign a console or not - ensure this is checked. (It's been a while since I used Eclipse so I can't give specific instructions I'm afraid).

If that doesn't work, then something that will definitely do this job is starting your application in debug mode, then connect to the process with Eclipse. Search for "eclipse remote debugging" if you're not sure how to do this.

Furthermore, in general it is a bad idea to require a console to be assigned as this very much impacts the flexibility of your application - as you've just discovered. Many ways of invoking Java will not assign a console, and your application is unusable in these instances (which is bad). Perhaps you could alternatively allow arguments to be specified on the command line. (If you're testing the console input specifically then fair enough, but it would potentially be useful for people to be able to invoke your application from scripts and/or on headless servers, so this sort of flexible design is almost always a good idea. It often leads to better-organised code, too.)


That is right.

You will have to run the application outside of Eclipse. Look at the launcher configuration panels within Eclipse and see if you can spot the option that says to run the command in a separate JVM.


add -console in your program arguments to start OSGi console

참고URL : https://stackoverflow.com/questions/4203646/system-console-returns-null

반응형