java调用python源代码:
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ModelUser {
public static void main(String[] args) throws IOException, InterruptedException {
String exe = "python ";
String command = "C:\\Users\\zbw\\Desktop\\predictor\\predictor1.py";#.py文件路径
String content = "公诉机关指控";#传入python代码参数内容
String[] cmdArr = new String[] {exe, command, content};
Process process = Runtime.getRuntime().exec(cmdArr);
InputStream is = process.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readLine());
String str = dis.readLine();
process.waitFor();
System.out.println(str);
}
}
返回乱码处理借鉴:https://blog.csdn.net/u010829418/article/details/50488005
if __name__ == '__main__': cnn_model=Predictor() content=[argv[1].decode('GBK').encode('utf-8')]#处理乱码 print(content) result=cnn_model.predict(content) print(result)
#上述main函数为我调用cnn模型代码