0
点赞
收藏
分享

微信扫一扫

Python中使用jpype调用Jar包中的方法

程序员伟杰 2022-01-13 阅读 100

安装

pip install jpype1(注意要加后边这个1)

使用

基本流程如下:

  1. 使用jpype开启jvm
  2. 加载java类
  3. 调用java方法
  4. 关闭jvm

说明

我这里是在Python中使用Java的第三方抽象语法树包JavaParser(Python中的javalang实在太难用了),实现得到一个类文件中的所有的方法的功能

代码

Python代码:

import jpype
import os
import json

if __name__ == '__main__':
    # 加载jar包
    jarpath = os.path.join(os.path.abspath('.'),'D:/study/hdu-cs-learning/Paper/TD/BuildDataset/target/BuildDataset.jar')
    # 获取jvm.dll的默认文件路径
    jvmPath = jpype.getDefaultJVMPath()
    # 开启虚拟机
    jpype.startJVM(jvmPath, '-ea', '-Djava.class.path=%s' % (jarpath))
    # 加载java类(参数名是java的长类名)
    javaClass = jpype.JClass('scluis.API')
    # 调用类中的方法
    class_file_path = 'D:/study/TD/OpenSourceProject/JFreeChart/source/org/jfree/chart/fx/ChartViewer.java'
    # 这里是调用scluis.API类的静态方法getMethods,如果调用实例方法,则需要先实例化java对象,即javaInstance = javaClass(),在使用实例调用
    file_methods_str = javaClass.getMethods(class_file_path)
    # 解析返回值,这里的返回值是json数组
    methods_list = ""
    if file_methods_str != "":
        methods_list = json.loads(str(file_methods_str))
    # 关闭虚拟机
    jpype.shutdownJVM()

API.java:

package scluis;
import com.github.javaparser.ParseException;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Random;


public class API {
    public static void main(String[] args) {
    	//main方法是为了测试Java代码即,getMethods,main方法不是必须的
        String filePath="D:/study/OpenSourceProject/SQuirrel/app/src/net/sourceforge/squirrel_sql/client/gui/HelpViewerWindow.java";
        String methods=getMethods(filePath);
        System.out.println(methods);
    }
    public static String getMethods(String filePath){
        String res="";
        try {
            Parser fileParser = new Parser();
            res= fileParser.getFileMethods(filePath);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return res;
    }
}

Parser.java(内含Java返回值格式)

package scluis;

import com.alibaba.fastjson.JSON;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.printer.DotPrinter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Parser {

    private CompilationUnit m_CompilationUnit;


    public CompilationUnit getParsedFile() {
        return m_CompilationUnit;
    }

    public String getFileMethods(String filePath) throws ParseException, IOException {
        String methodJson = "";
        try {
            m_CompilationUnit = StaticJavaParser.parse(new File(filePath));
            FunctionVisitor functionVisitor = new FunctionVisitor();

            functionVisitor.visit(m_CompilationUnit, null);
            ArrayList<MethodDeclaration> nodes = functionVisitor.getMethodDeclarations();
            ArrayList<Method> methodList = new ArrayList<>();
            for (int i = 0; i < nodes.size(); i++) {

                MethodDeclaration methodDeclaration = nodes.get(i);
                //起止行
                int startLine = methodDeclaration.getRange().get().begin.line;
                int endLine = methodDeclaration.getRange().get().end.line;
                //方法代码
                String method = methodDeclaration.removeComment().toString();

                Method m = new Method(startLine, endLine, method);
                methodList.add(m);
            }
            methodJson = JSON.toJSONString(methodList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return methodJson;
    }
}

举报

相关推荐

0 条评论