Python中使用jpype調(diào)用Jar包中的實現(xiàn)方法
使用jpype調(diào)用Jar包中的實現(xiàn)方法
安裝
pip install jpype1(注意要加后邊這個1)
使用
基本流程如下:
- 使用jpype開啟jvm
- 加載java類
- 調(diào)用java方法
- 關(guān)閉jvm
說明
我這里是在Python中使用Java的第三方抽象語法樹包JavaParser(Python中的javalang實在太難用了),實現(xiàn)得到一個類文件中的所有的方法的功能
代碼
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的默認(rèn)文件路徑
? ? jvmPath = jpype.getDefaultJVMPath()
? ? # 開啟虛擬機
? ? jpype.startJVM(jvmPath, '-ea', '-Djava.class.path=%s' % (jarpath))
? ? # 加載java類(參數(shù)名是java的長類名)
? ? javaClass = jpype.JClass('scluis.API')
? ? # 調(diào)用類中的方法
? ? class_file_path = 'D:/study/TD/OpenSourceProject/JFreeChart/source/org/jfree/chart/fx/ChartViewer.java'
? ? # 這里是調(diào)用scluis.API類的靜態(tài)方法getMethods,如果調(diào)用實例方法,則需要先實例化java對象,即javaInstance = javaClass(),在使用實例調(diào)用
? ? file_methods_str = javaClass.getMethods(class_file_path)
? ? # 解析返回值,這里的返回值是json數(shù)組
? ? methods_list = ""
? ? if file_methods_str != "":
? ? ? ? methods_list = json.loads(str(file_methods_str))
? ? # 關(guān)閉虛擬機
? ? 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(內(nèi)含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;
? ? }
}jpype調(diào)用jar包“Class xx not found“問題
環(huán)境
- Java 1.8 (64位)
- Python 2.7.9 (32位)
- Jpype 0.5.4.2
- Pycharm
代碼
import jpype
from jpype import *
# 該目錄下有需要調(diào)用的jar包 pak.jar
jvmArg = "-Djava.ext.dirs=D:/1_Workspace/jpype_test/"
if not jpype.isJVMStarted():
jvmPath = jpype.getDefaultJVMPath()
jpype.startJVM(jvmPath,"-ea", jvmArg)
# pak.jar中包含類com.abc.EFG
jd = JClass("com.abc.EFG")
instance = jd()
print(instance.getName())
jpype.shutdownJVM()
問題
執(zhí)行以上代碼,報錯如下。Class com.abc.EFG not found

檢查點
1.在pak.jar包中有com.abc.EFG類
2.EFG依賴的jar包都在同級目錄下
3.Jpype使用沒有問題:調(diào)用System.out.println可以打印
jprint = java.lang.System.out.println
jprint("xxx") #輸出xxx試著從環(huán)境上找原因,也許和Java版本有關(guān)。
首先查找pak.jar包的編譯jdk版本
使用以上方法獲得輸出是 52,即對應(yīng)java 1.8,與本機Java版本相符。
但是回頭一想其實在Pycharm中并沒有配置過Java的版本。
那么打印一下jvmPath,獲得的路徑是C:\Program Files (x86)\Java\jre6\bin\client\jvm.dll,=> 使用的是 jre1.6(32位)。
此時去Java1.8目錄找,發(fā)現(xiàn)server目錄下有jvm.dll,不是在client目錄。管他的,嘗試直接指定存在的jvm.dll路徑。
# ... # jvmPath = jpype.getDefaultJVMPath() jvmPath = r'D:\java\jdk1.8\jre\bin\server\jvm.dll' jpype.startJVM(jvmPath,"-ea", jvmArg) # ...
到這里也許有人的問題可以解決了,但我的情況是還是報錯…
解決
最后裝了jdk1.8 32位?。?!,發(fā)現(xiàn)在client目錄下有jvm.dll文件了
再次修改代碼,執(zhí)行通過。

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)自動發(fā)消息自定義內(nèi)容的操作代碼
這篇文章主要介紹了Python實現(xiàn)自動發(fā)消息自定義內(nèi)容的操作代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Python實現(xiàn)圖形用戶界面和游戲開發(fā)的方法和技巧
GUI圖形用戶界面編程,我們可以通過python提供的豐富的組件,快速的實現(xiàn)使用圖形的界面和用戶交互, GUI編程類似于“搭積?”,將?個個組件(Widget)放到窗?中,這篇文章主要給大家介紹了基于Python的GUI圖形用戶界面編程的相關(guān)資料,需要的朋友可以參考下2023-05-05
Python+Tkinter實現(xiàn)經(jīng)典井字棋小游戲
Tkinter是內(nèi)置到Python安裝包中的,只要安裝好Python之后就能import?Tkinter,而且IDLE也是用Tkinter編寫而成的。本文將用Tkinter編寫經(jīng)典的井字棋小游戲,需要的可以參考一下2022-03-03
python3 中文亂碼與默認(rèn)編碼格式設(shè)定方法
今天小編就為大家分享一篇python3 中文亂碼與默認(rèn)編碼格式設(shè)定方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python實現(xiàn)數(shù)據(jù)庫跨服務(wù)器遷移
這篇文章主要為大家詳細介紹了Python實現(xiàn)數(shù)據(jù)庫之間的數(shù)據(jù)遷移,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法
這篇文章主要介紹了python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03

