一梯嗽、通過jython
1快鱼、通過jython的需要依賴jython庫哟楷,即在maven的pom.xml里面配置
<dependency>
<groupId>org.python</groupId>
<artifactId>jthon-standalone</artifactId>
<version>x.x.x(如2.7.0)</version>
</dependency>
2馍刮、import org.python.util.PythonInterpreter
????????????PythonInterpreter pythonInterpreter = new PythonInterpreter();
? ? ? ? ? ? pythonInterpreter.exec("");//直接調(diào)python腳本
? ? ? ? ? ? pythonInterpreter.execfile("");//調(diào)python文件
3簸搞、問題茉贡,用jython好像沒辦法調(diào)用 引入其他模塊的python代碼塞栅,會報錯,類似can‘t find moudle **** 比如can‘t find moudle named sys腔丧,
二放椰、通過Process 這個是java自帶的,好像可以調(diào)用各種的腳本
import java.io.*;
import org.python.util.PythonInterpreter
public class runSerial{
? ? public static void main(String[] args) {
? ? ? ? String[] arguments = new String[] {"python3", "/Users/***/Documents/bserial","/dev/tty.usbserial-1420","2000000","hello\n"};
? ? ? ? try {
? ? ? ? ? ? Process process = Runtime.getRuntime().exec(arguments);
? ? ? ? ? ? BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
? ? ? ? ? ? String line = null;
? ? ? ? ? ? while ((line = in.readLine()) != null) {
? ? ? ? ? ? System.out.println(line);
? ? ? ? ? ? in.close();
? ? ? ? ? ? }
? ? ? ? ? ? int re = process.waitFor();
? ? ? ? ? ? System.out.println(re);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? // TODO: handle exception
? ? ? ? }
? ? }
}
參考:https://blog.csdn.net/qq_35240555/article/details/123563947愉粤,https://baijiahao.baidu.com/s?id=1712788796946022908&wfr=spider&for=pc砾医,https://blog.csdn.net/zjq852533445/article/details/126598270