Eclipse IDE에서 java.io.Console 지원
Eclipse IDE를 사용하여 Java 프로젝트를 개발, 컴파일 및 실행합니다. 오늘 저는이 java.io.Console
클래스 를 사용하여 출력과 더 중요한 사용자 입력을 관리 하려고합니다 .
문제는 응용 프로그램이 Eclipse를 "통해"실행될 때 System.console()
반환 된다는 것 null
입니다. Eclipse는 익숙한 콘솔 창을 사용하여 최상위 프로세스가 아닌 백그라운드 프로세스에서 프로그램을 실행합니다.
Eclipse가 프로그램을 최상위 프로세스로 실행하도록 강제하거나 최소한 JVM이 인식 할 콘솔을 만드는 방법이 있습니까? 그렇지 않으면 프로젝트를 jar up하고 Eclipse 외부의 명령 줄 환경에서 실행해야합니다.
Eclipse에서 단계별 디버깅을 사용할 수 있기를 원한다고 가정합니다. JRE 클래스 경로의 bin 디렉토리에 빌드 된 클래스를 설정하여 외부에서 클래스를 실행할 수 있습니다.
java -cp workspace\p1\bin;workspace\p2\bin foo.Main
원격 디버거를 사용하여 디버깅하고 프로젝트에 빌드 된 클래스 파일을 활용할 수 있습니다.
이 예에서 Eclipse 프로젝트 구조는 다음과 같습니다.
workspace\project\
\.classpath
\.project
\debug.bat
\bin\Main.class
\src\Main.java
1. 디버그 모드에서 JVM 콘솔 시작
debug.bat 는 cmd.exe 콘솔 에서 외부 적으로 실행해야하는 Windows 배치 파일입니다 .
@ECHO OFF
SET A_PORT=8787
SET A_DBG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=%A_PORT%,server=y,suspend=y
java.exe %A_DBG% -cp .\bin Main
인수에서 디버그 포트는 8787 로 설정되었습니다 . 은 중단 = y를 인수 디버거가 첨부 될 때까지 대기하도록 JVM을 알려줍니다.
2. 디버그 시작 구성 만들기
Eclipse에서 디버그 대화 상자 (실행> 디버그 대화 상자 열기 ...)를 열고 다음 설정 으로 새 원격 Java 애플리케이션 구성을 작성하십시오.
- 프로젝트 : 프로젝트 이름
- 연결 유형 : 표준 (소켓 부착)
- 호스트 : localhost
- 포트 : 8787
3. 디버깅
따라서 앱을 디버깅 할 때마다해야 할 일은 다음과 같습니다.
- 중단 점을 설정
- 콘솔에서 배치 파일 시작
- 디버그 구성 시작
이 문제는 버그 122429 에서 추적 할 수 있습니다 . 여기에 설명 된대로 추상화 계층을 사용하여 응용 프로그램에서이 문제를 해결할 수 있습니다 .
내가 사용하는 해결 방법은 Eclipse를 사용할 때 Console 대신 System.in/System.out을 사용하는 것입니다. 예를 들어 다음 대신 :
String line = System.console().readLine();
당신이 사용할 수있는:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();
이 문제가 발생하는 이유는 eclipse가 앱을 시스템 콘솔의 최상위 프로세스가 아닌 백그라운드 프로세스로 실행하기 때문입니다.
클래스를 직접 구현할 수 있습니다. 다음은 그 예입니다.
public class Console {
BufferedReader br;
PrintStream ps;
public Console(){
br = new BufferedReader(new InputStreamReader(System.in));
ps = System.out;
}
public String readLine(String out){
ps.format(out);
try{
return br.readLine();
}catch(IOException e)
{
return null;
}
}
public PrintStream format(String format, Object...objects){
return ps.format(format, objects);
}
}
http://www.stupidjavatricks.com/?p=43 에서 이에 대한 내용을 찾았습니다 .
그리고 슬프게도 콘솔이 최종이기 때문에이를 확장하여 system.in 및 system.out 주위에 래퍼를 만들 수 없습니다. Eclipse 콘솔 내부에서도 여전히 액세스 할 수 있습니다. 이것이 아마도 eclipse가 아직 콘솔에 이것을 연결하지 않은 이유 일 것입니다.
Setter없이 System.console 이외의 콘솔을 얻는 다른 방법을 원하지 않는 이유는 이해합니다.하지만 다른 사람이 클래스를 재정의 할 수 없도록하는 이유를 이해할 수 없습니다. 모의 / 테스트 콘솔 ...
Another option is to create a method to wrap up both options, and "fail over" to the System.in method when Console isn't available. The below example is a fairly basic one - you can follow the same process to wrap up the other methods in Console (readPassword, format) as required. That way you can run it happily in Eclipse & when its deployed you get the Console features (e.g. password hiding) kicking in.
private static String readLine(String prompt) {
String line = null;
Console c = System.console();
if (c != null) {
line = c.readLine(prompt);
} else {
System.out.print(prompt);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
line = bufferedReader.readLine();
} catch (IOException e) {
//Ignore
}
}
return line;
}
As far as I can tell, there is no way to get a Console object from Eclipse. I'd just make sure that console != null, then JAR it up and run it from the command line.
There seems to be no way to get a java.io.Console object when running an application through Eclipse. A command-line console window is not opened with the application, as it is run as a background process (background to Eclipse?). Currently, there is no Eclipse plugin to handle this issue, mainly due to the fact that java.io.Console is a final class.
All you can really do is test the returned Console object for null and proceed from there.
This link offers alternatives to using System.console(). One is to use a BufferedReader wrapped around System.in, the second is to use a Scanner wrapped around System.in.
Neither are as concise as console, but both work in eclipse without having to resort to debug silliness!
Let's say your Eclipse workspace is C:\MyWorkspace, you created your java application inside a maven project MyProject, and your Java main class is com.mydomain.mypackage.MyClass.
In this case, you can run your main class that uses System.console()
on the command line:
java -cp C:\MyWorkspace\MyProject\target\classes com.mydomain.mypackage.MyClass
NB1: if it's not in a maven project, check the output folder in project properties | Java Build Path | Source. It might not be "target/classes"
NB2: if it is a maven project, but your class is in src/test/java, you'll likely have to use "target\test-classes" instead of "target\classes"
참고URL : https://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide
'Programing' 카테고리의 다른 글
커밋 메시지는 현재 또는 과거 시제로 작성해야합니까? (0) | 2020.08.17 |
---|---|
Visual Studio에서 사용할 새 언어를 만드는 방법 (0) | 2020.08.17 |
최적화가 활성화 된 다른 부동 소수점 결과-컴파일러 버그? (0) | 2020.08.17 |
R 도움말 페이지에서 "실행되지 않음"은 무엇을 의미합니까? (0) | 2020.08.17 |
Golang의 글로벌 로깅에 대한 올바른 접근 방식 (0) | 2020.08.17 |