[Python] Python3 call java code

Environment: Ubuntu16.04 desktop

Ubuntu detailed tutorial to install the java: https://www.cnblogs.com/ttkl/p/11933884.html

Installation JPype1

pip3 install JPype1

 

1, java files compiled into class files

javac -encoding UTF-8 -Djava.ext.dirs=/(*.jar) *.java

 

2, packaging class files

jar cvf *.jar *.class

 

Sample code:

# -*- coding:utf-8 -*-
from jpype import *
import os

# 启动Java环境
startJVM("/opt/java/jdk1.8.0_231/jre/lib/amd64/server/libjvm.so", "-ea", \
         "-Djava.class.path=%s" % ("*.jar"), "-Djava.ext.dirs=%s" % ("../jar/(*.jar)"))

# Load Custom Class of the Java 
JClass = JClass ( " HmacUtil " )
jc = JClass()

# Call HmacUtil class main () method to get Sign 
the try :
    sign = jc.main()
except Exception as e:
    print(e)

print(sign)

# Close the Java environment 
shutdownJVM ()

 

Guess you like

Origin www.cnblogs.com/ttkl/p/11939113.html