I want to execute a python script through java. I used below sample code for that. the problem is when I execute python script through commandline it save logs in to log_test.log file. but when I execute from java logs doesn't write in to the log file I'm using python 3.7 and java 1.8
Python script
import logging
logger = logging.getLogger('log_test')
logger.setLevel(logging.INFO)
fh = logging.FileHandler('log_test.log')
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('info message')
Java code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) throws ScriptException, IOException {
Process p = Runtime.getRuntime().exec("python ./log_test.py");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("output");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
}
}
Change Python path and script path to absolute path
And change the file path of logging.FileHandler
to the absolute path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.