首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3:如何从python运行java类文件

Python3:如何从python运行java类文件
EN

Stack Overflow用户
提问于 2018-01-12 13:47:08
回答 1查看 1.8K关注 0票数 1

我从以下链接阅读了JPype文档:http://jpype.readthedocs.io/en/latest/,但我不确定是否可以使用JPype,或者最好选择另一种方式从python3运行java类。此外,我必须指出,我对使用JPype感到有点困惑。我做了这个:

代码语言:javascript
复制
    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()
  • 但是它有问题,并告诉我这个错误: 7,在导入jpype as jp ModuleNotFoundError: No模块名为“jpype”

========================================================================

  • 我必须指出,我的java类位于python路径中,我的java类是:

公共类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());}}

  1. 我不知道为什么这不管用!我读过jython,但我不能使用jython,因为我不太懂。你能帮我吗:
  2. 如何从python 2运行java类--将执行的输出保存在文本文件3中--将可能的错误消息保存在另一个文本文件中,然后终止。谢谢
EN

回答 1

Stack Overflow用户

发布于 2018-01-12 13:52:17

我通常使用JYthon。在Jython应用程序中使用Java就像在Jython脚本中使用外部Jython模块一样无缝。

下面是一个简单的例子:

代码语言:javascript
复制
from java.lang import Math

Math.max(4, 7)

Math.pow(10,5)

from java.lang import System as javasystem
javasystem.out.println("Hello")
Hello
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48227571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档