我从以下链接阅读了JPype文档:http://jpype.readthedocs.io/en/latest/,但我不确定是否可以使用JPype,或者最好选择另一种方式从python3运行java类。此外,我必须指出,我对使用JPype感到有点困惑。我做了这个:
import urllib.request
import os
import tempfile
import sys
import fileinput
import logging
import JPype as jp
# here i have python code and do something else
"""
my python code
"""
#from this point till end of the code i did for my question
logging.basicConfig(filename="ERROR.txt", level= logging.ERROR)
try:
logging.debug('we are in the main try loop')
jp.startJVM("C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest.java", "-ea")
test_class = jp.JClass("ClassWithTest")
a = testAll()
file_java_class = open("OUTPUT.txt", "w")
file_java_class.write(a)
except Exception as e1:
logging.error(str(e1))
jp.shutdownJVM()========================================================================
公共类ClassWithTest{公共ClassWithTest(){ /* Va invocato */ System.out.println(insertLength(“测试准备好运行”));}公钥testAbs(){ /* Va invocato */ System.out.println(insertLength(“调用”+ClassWithTest))System.out.println(“abs(-2)= 2:”+ testResult(Math.abs(-2),2));System.out.println(“abs(-3)= -2:”+ testResult(Math.abs(-3),-2));} public testRound(){ /* Va invocato */ System.out.println(insertLength(“调用”+System.out.println(insertLength(“循环(0.7)= 1:”+ testResult(Math.round(0.7),1);Thread.currentThread().getStackTrace()1.getMethodName()));(insertLength(“insertLength(0.2)= 1:”+ testResult(Math.round(0.2),1);}公开无效testMin(){ /* Va invocato */ System.out.println(insertLength(“调用”+Math.round))System.out.println(“min(7,3)= 3:”+ testResult(Math.min(7,3),3));System.out.println(insertLength("min(5,7) = 7:“+ testResult(Math.min(5,7),7));System.out.println(“max(7,14)= 14:”+ testResult(Math.max(7,14),14)),System.out.println(“max(5,7)= 5:”+testResult(5,7),5);}公共TestDiv(){ /* Non和andreabbe chiamato */ System.out.println(insertLength(“调用”+System.out.println(insertLength,“14/2= 7:”+ testResult(14 / 7,3));// assert(14 /0 == 10));} public testMult(int x){ /* Non和andreabbe chiamato */ System.out.println(insertLength(“调用”+/* System.out.printf("2 * %d = %s:",x,testResult(2 * x,2*x));//断言(False));} public otherTest(){ /* Non和andreabbe chiamato */ System.out.println(insertLength(“调用”+insertLength System.out.printf("1 = 1:",testResult(1,1);//断言(False));}私有Thread.currentThread().getStackTrace()1.getMethodName()));testAll(){ /* Non essere chiamato */ System.out.println(insertLength(“调用”+ testAbs());testRound();testMin();testMax();}私有静态字符串testResult(双重结果,双重预期)){返回(预期结果==?"ok“:”no“);} public静态字符串insertLength(Object obj){ String line =obj.toString();返回String.format("%d> %s",line.length(),line);}公共静态void (String[] args){ (新ClassWithTest().testAll());}}
发布于 2018-01-12 13:52:17
我通常使用JYthon。在Jython应用程序中使用Java就像在Jython脚本中使用外部Jython模块一样无缝。
下面是一个简单的例子:
from java.lang import Math
Math.max(4, 7)
Math.pow(10,5)
from java.lang import System as javasystem
javasystem.out.println("Hello")
Hellohttps://stackoverflow.com/questions/48227571
复制相似问题