java运行python文件(python在java上运行)
java怎么点用python脚本?
首先得声明一下,java是java,python是python,你用java得环境跑python这不是找麻烦吗,但是并不是说不行,java有一个Jpython得库,你可以下载一下,这方面原理设计jni技术,建议了解一下,如果单纯想运行一个脚本可以找Jpython得api文档看看
如何在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());
用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 module named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();
java调用python权限不足
java调用python权限不足你的账号没有文件夹的权限呗,不知道你是用什么容器调用的python。如果是aspx,它是用另外一个系统账号来运行的,和你当前登录的账号是不一样的,权限也不一样,不能访问很正常。你把imgs设为所有人都有权限读写就行了