https://stackoverflow.com/questions/10097491/call-and-receive-output-from-python-script-in-java
Call and receive output from Python script in Java?
What's the easiest way to execute a Python script from Java, and receive the output of that script? I've looked for different libraries like Jepp or Jython, but most appear out of date. Another pro...
stackoverflow.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "ps -ef" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("python test.py");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
|
cs |
'[공부용]참고 사이트 모음 > [java]' 카테고리의 다른 글
자바 개발자라면 필독 도서! 이펙티브 자바(Effective Java) (0) | 2022.01.21 |
---|---|
자바 파일 변경 감지, 와치서비스를 이용한 파일 변경 알림 받기(WatchService, WatchKey) (0) | 2021.04.12 |
JAVA HTTP POST 전송 예제 (0) | 2021.03.25 |
자바 ftp 라이브러리 정리 jsch sftp (0) | 2021.03.11 |
이미지 암호화 (0) | 2021.02.26 |