简介:
JPype is a Python module to provide full access to Java from within Python
Jpype是一个Python模块,用于从Python内部提供对Java的完全访问。
github:https://github.com/jpype-project/jpype/
文档:https://jpype.readthedocs.io/en/latest/index.html
安装
pip install JPype1
1、调用java内置函数
from jpype import * startJVM(getDefaultJVMPath(), "-ea", convertStrings=False) java.lang.System.out.println("hello world") shutdownJVM()
2、调用用户自定义java类
Demo.java
public class Demo{ public int add(int a, int b){ return a + b; } public static void main(String[] args) { Demo demo = new Demo(); System.out.println(demo.add(1, 1)); } }
编译测试
$ javac Demo.java $ java Demo 2
通过python调用java类方法
# -*- coding: utf-8 -*- import jpype jpype.startJVM(convertStrings=False) Demo = jpype.JClass('Demo') demo = Demo() print(demo.add(1, 1)) # 2 jpype.shutdownJVM()
备注:两文件放同一目录下