美文网首页
python调用jpype

python调用jpype

作者: 王国的荣耀 | 来源:发表于2020-10-29 18:20 被阅读0次

python调用jpype开启虚拟机方法startJVM()报错:OSError: JVM is already started和JVM cannot be restarted。

设置try except pass 可解决。

    jvm_path = getDefaultJVMPath()
    print(jvm_path)
    dependence_dirs = list()
    curDir = os.getcwd()
    dependence_dirs.append(curDir +"/server")
    if platform.system() == "Windows":
        java_dirs = ";".join(dependence_dirs)
    else:
        java_dirs = ":".join(dependence_dirs)
    print(java_dirs)

    jar_path = "/path//test.jar"
    try:
        startJVM(jvm_path, "-ea", "-Djava.class.path=%s" % jar_path, "-Djava.ext.dirs=%s" % java_dirs, convertStrings=False)
    except:
        pass

    gclass= JClass('com.111.222.333.444.test')
    gclassInstance = gclass()
    data = gclassInstance.getdata(a,b)
    print(data)
    return data

python flask post接口

server.py

from flask import Flask, request, jsonify
import json
 
app = Flask(__name__)
app.debug = True
 
 
@app.route('/add/student/',methods=['post'])
def add_stu():
    if  not request.data:   #检测是否有数据
        return ('fail')
    student = request.data.decode('utf-8')
    student_json = json.loads(student)
    return jsonify(student_json)
 
if __name__ == '__main__':
    app.run(host='192.168.1.154',port=1234)

test.py
``
import requests,json

data = {
'id':1,
'name':'lily',
'age':11,
'birthplace':'san',
'grade':123
}
url = 'http://192.168.1.154:1234/add/student/'

r = requests.post(url,data=json.dumps(data))
print(r.json())

相关文章

网友评论

      本文标题:python调用jpype

      本文链接:https://www.haomeiwen.com/subject/tyllvktx.html