JAVA调用Python

JAVA调用Python的方法:

  1. 通过 **PythonInterpreter.execfile() **方法实现;
  2. 通过 Runtime.getRuntime().exec(arg1) 方法实现;

使用第一种方法遇到的问题

此时环境都配置好了

解决方法

出现这个问题,查了很多资料都没有解决。最终换成了第二种方法解决。

完整代码

JAVA

	Process pr;
	try {
		String[] args1 = new String[] { "python", "E:\\learn\\datacalculation.py", "10", "15"};
		pr = Runtime.getRuntime().exec(args1);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				pr.getInputStream()));
		String line;
		while ((line = in.readLine()) != null) {
			System.out.println(line);
		}
		in.close();
		pr.waitFor();
	} catch (IOException | InterruptedException e) {
		e.printStackTrace();
	}

	/*try {
		PythonInterpreter interpreter = new PythonInterpreter();
		interpreter.execfile("E:\\learn\\datacalculation.py");
		PyFunction pyFunction = interpreter.get("test", PyFunction.class);
		int a = 5, b = 15;
		PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
		System.out.println("the anwser is: " + pyobj);
	} catch (Exception e) {
		e.printStackTrace();
	}*/

Python

# coding=utf-8
import sympy as sp  # 导入sympy包

x = sp.Symbol('x')  # 定义符号变量
f = 0.137*x**-3 +0.119*x**-2 + 0.269*x**-1 - 0.019 - 44000/101300  # 定义要求解的一元三次方程
x = sp.solve(f)    # 调用solve函数求解方程
#str(x)           # solve函数的返回结果是一个列表
print('x' + str(x))

具体原因还望指点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值