본문 바로가기

개발/Java

[Swing] 윈도우 탐색기 또는 커맨드(cmd.exe) 실행하는 방법

728x90
반응형

JAVA/Swing 환경에서 윈도우 탐색기 또는 커맨드창을 실행하면 유용할 것 같아 devcode-stuio JavaProperties 내용을 조회하는 기능에 적용하였다.

devcode-studio - https://github.com/wyleedp/devcode/tree/develop/devcode-studio

[devcode-stuio] JavaProperties 파일탐색기/터미널 열기

 

윈도우 파일탐색기 명령어

# C:\Windows\explorer.exe /e/root/select,<이동경로>
C:\Windows\explorer.exe /e/root/select,c:\Windows

윈도우 커맨드창 실행 명령어

# C:\Windows\system32\cmd.exe /C start "<이동경로>" /D "<이동경로>"
C:\Windows\system32\cmd.exe /C start "C:\Users\wylee\AppData\Local\Temp\" /D "C:\Users\wylee\AppData\Local\Temp\"

 

자바의 Runtime.getRuntime 을 이용하여 해당 명령어를 실행하면 윈도우의 exe 파일을 실행할 수 있다.

 

참고소스

// 탐색기 명령어 예)
//   - c:\Windows\explorer.exe /e/root/select,c:\Windows
String explorerCommand = System.getenv("SystemRoot") + FILE_SEPARATOR + "explorer.exe";

File explorerCommandFile = new File(explorerCommand);
if(!explorerCommandFile.exists() || !explorerCommandFile.canExecute()) {
	JOptionPane.showMessageDialog(getRootPane(), "explorer.exe 파일이 존재하지 않거나 실행권한이 없습니다.", "확인", JOptionPane.WARNING_MESSAGE);
	return;
}

String[] commnds = new String[]{explorerCommand, "/e/root/select,"+value};
logger.info("ExplorerCommand : {}", StringUtils.join(commnds, " "));

Runtime rt = Runtime.getRuntime();
Process process = null;  

try {
    process = rt.exec(commnds);
    List<String> inputLines = IOUtils.readLines(process.getInputStream(), "EUC-KR");
    List<String> errorLines = IOUtils.readLines(process.getErrorStream(), "EUC-KR");
    
    if(inputLines != null && inputLines.size() > 0) {
    	logger.error("INPUT - {}", StringUtils.join(inputLines, "<LS>"));
    }
    
    if(errorLines != null && errorLines.size() > 0) {
    	logger.error("ERROR - {}", StringUtils.join(errorLines, "<LS>"));
    }
} catch (Exception ex) {
    logger.error("ERROR", ex);
}finally {
	if(process != null) {
		process.destroy();
	}
}


// 터미널 명령어 예)
//   - C:\Windows\system32\cmd.exe /C start "C:\Users\wylee\AppData\Local\Temp\" /D "C:\Users\wylee\AppData\Local\Temp\"
String cmdCommand = System.getenv("ComSpec");
if(StringUtils.isBlank(cmdCommand)) {
	cmdCommand = System.getenv("SystemRoot") + FILE_SEPARATOR + "System32" + FILE_SEPARATOR + "cmd.exe";
}

File cmdCommandFile = new File(cmdCommand);
if(!cmdCommandFile.exists() || !cmdCommandFile.canExecute()) {
	JOptionPane.showMessageDialog(getRootPane(), "cmd.exe 파일이 존재하지 않거나 실행권한이 없습니다.", "확인", JOptionPane.WARNING_MESSAGE);
	return;
}

value = StringUtils.wrap(value, "\"");
String[] commnds = new String[]{cmdCommand, "/C", "start", value, "/D", value};
logger.info("Command : {}", StringUtils.join(commnds, " "));

Runtime rt = Runtime.getRuntime();
Process process = null;

try {
    process = rt.exec(commnds);
    List<String> inputLines = IOUtils.readLines(process.getInputStream(), "EUC-KR");
    List<String> errorLines = IOUtils.readLines(process.getErrorStream(), "EUC-KR");
    
    if(inputLines != null && inputLines.size() > 0) {
    	logger.error("INPUT - {}", StringUtils.join(inputLines, "<LS>"));
    }
    
    if(errorLines != null && errorLines.size() > 0) {
    	logger.error("ERROR - {}", StringUtils.join(errorLines, "<LS>"));
    }
} catch (Exception ex) {
    logger.error("ERROR", ex);
}finally {
	if(process != null) {
		process.destroy();
	}
}

 

728x90
반응형