카테고리 없음
java로 exe파일 실행 후 결과값 반환하기
숲별
2023. 12. 11. 17:06
728x90
https://bono915.tistory.com/entry/JAVA-%ED%8C%8C%EC%9D%BC-%EC%8B%A4%ED%96%89
process = rt.exec(String.format("create_exe_v4.exe %s", file));
Process process;
if (isWindows)
{
process = Runtime.getRuntime()
.exec(String.format("cmd.exe /c dir %s", homeDirectory));
}
else
{
process = Runtime.getRuntime()
.exec(String.format("sh -c ls -l %s", homeDirectory));
}
https://yjkim97.tistory.com/70
java.lang....
Runtime, Process
https://byul91oh.tistory.com/249
public String runExe() {
Runtime rt = Runtime.getRuntime();
// 실행할 파일의 경로
String tmpfile = System.getProperty("user.home");
System.out.println("=========directory : "+ tmpfile);
String file = "C:\\Users\\stlogic\\git\\AI\\AI\\create_exe_v4.exe";
Process pro;
StringBuilder result = new StringBuilder();
try {
System.out.println("=========START : Use Runtime=========");
System.out.println("=========directory : "+ file);
pro = rt.exec(file);
pro.waitFor();
Reader reader = new InputStreamReader(pro.getInputStream());
for (int data = reader.read(); data != -1; data = reader.read()) {
result.append((char)data);
}
System.out.println("=========result : " + result.toString());
// System.out.println(new InputStreamReader(pro.getInputStream()));
// StreamGobbler streamGobbler = new StreamGobbler(pro.getInputStream(), System.out::println);
// Executors.newSingleThreadExecutor().submit(streamGobbler);
System.out.println("=========END : Use Runtime=========");
}catch (IOException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
return result.toString();
}
}
python으로 만들고 shell 통해서 통신
아니면 https://d2.naver.com/helloworld/1113548 와 같이 프로세스로 실행
ProcessBuilder를 이용해서 같은 패키지안에 있는 파이썬 파일을 실행시킨다.
첫 인자는 python.exe의 경로인데 환경변수 설정 때문에 python 명령만 적어줘도 실행 가능
세번쩨 인자는 파이썬 파일에 줄 인자