当前位置:首页 » 编程软件 » Java运行脚本优化

Java运行脚本优化

发布时间: 2025-03-07 06:29:38

1. 怎么使用java运行python脚本

1.直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本


2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();


3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();

2. 如何在java工程里运行一个python脚本

可以使用jython


方法参考如下

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.execfile("uar/local/xxx.py");

//pyFunction :py中方法名

PyFunction func = (PyFunction)interpreter.get("pyFunction",PyFunction.class);

Integer a = 1

Integer b = 2

// py中方法传参

PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));

System.out.println("anwser = " + pyobj.toString());

热点内容
centos安装php52 发布:2025-07-12 15:14:19 浏览:297
usb接口编程 发布:2025-07-12 15:14:19 浏览:214
算法学习心得 发布:2025-07-12 15:14:08 浏览:793
华为手机内核编译 发布:2025-07-12 15:13:13 浏览:837
汇编语言编译器masm 发布:2025-07-12 14:57:37 浏览:56
校园网服务器ip地址 发布:2025-07-12 14:55:02 浏览:238
如何用密码锁定 发布:2025-07-12 14:39:10 浏览:925
软件发布源码 发布:2025-07-12 14:29:34 浏览:179
sql函数和存储过程的区别 发布:2025-07-12 14:26:37 浏览:30
查看存储功空间 发布:2025-07-12 14:17:22 浏览:942