제가 이해한 것이 맞다면 Java에서 호출할 수 있도록 .py 파일만 전달드리면 될 것 같습니다.
만약 .py 파일 이름이 hello.py라고 한다면 자바에서는 간단하게는 아래와 같이 호출하면 되고
Process p = Runtime.getRuntime().exec("p..
제가 이해한 것이 맞다면 Java에서 호출할 수 있도록 .py 파일만 전달드리면 될 것 같습니다.
만약 .py 파일 이름이 hello.py라고 한다면 자바에서는 간단하게는 아래와 같이 호출하면 되고
Process p = Runtime.getRuntime().exec("python hello.py");
좀 더 복잡하게는 아래와 같이 호출하면 됩니다.
@Test
public void givenPythonScript_whenPythonProcessExecuted_thenSuccess()
throws ExecuteException, IOException {
String line = "python " + resolvePythonScriptPath("hello.py");
CommandLine cmdLine = CommandLine.parse(line);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(streamHandler);
int exitCode = executor.execute(cmdLine);
assertEquals("No errors should be detected", 0, exitCode);
assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", outputStream.toString()
.trim());
}
즉, python 파일을 커맨드 라인에서 실행할 수 있으면 이후 호출은 자바쪽에서 하면 될 것 같습니다.
댓글