java - Python3 : how to run java class file from python

标签 java python python-3.x

我从此链接阅读了 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()
  • 但它有问题并向我显示此错误:7,导入中 jpype as jp ModuleNotFoundError:没有名为“jpype”的模块

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

  • 我必须指出我的 java 类位于我的 python 路径和我的 java 中 类(class)是:
 public class ClassWithTest{
        public ClassWithTest(){
      /* Va invocato */
      System.out.println(insertLength("Tests ready to run"));
        }

        public void testAbs(){
      /* Va invocato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.println(insertLength("abs(-2) = 2: " + testResult(Math.abs(-2), 2)));
      System.out.println(insertLength("abs(-3) = -2: " + testResult(Math.abs(-3), -2)));
        }

        public void testRound(){
      /* Va invocato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.println(insertLength("round(0.7) = 1: " + testResult(Math.round(0.7), 1)));
      System.out.println(insertLength("round(0.2) = 1: " + testResult(Math.round(0.2), 1)));
        }

        public void testMin(){
      /* Va invocato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.println(insertLength("min(7,3) = 3: " + testResult(Math.min(7,3), 3)));
      System.out.println(insertLength("min(5,7) = 7: " + testResult(Math.min(5,7), 7)));
        }

        public void testMax(){
      /* Va invocato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.println(insertLength("max(7,14) = 14: " + testResult(Math.max(7,14), 14)));
      System.out.println(insertLength("max(5,7) = 5: " + testResult(Math.max(5,7), 5)));
        }

        public void TestDiv(){
      /* Non andreabbe chiamato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.println(insertLength("14 / 2 = 7: " + testResult(14 / 7, 3)));
      //  assert(14 / 0 == 10));
        }

        public void testMult(int x){
      /* Non andreabbe chiamato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.printf("2 * %d = %s:", x, testResult(2 * x, 2 * x)));
      //       assert(false));
        }

        public void otherTest(){
      /* Non andreabbe chiamato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      System.out.printf("1 = 1:", testResult(1, 1)));
      //       assert(false));
        }

        private void testAll(){
      /* Non dovrebbe essere chiamato */
      System.out.println(insertLength("Invocation of " + Thread.currentThread().getStackTrace()[1].getMethodName()));
      testAbs());
      testRound());
      testMin());
      testMax());
        }

        private static String testResult(double result, double expected){
      return (result == expected ? "ok" : "no"));
        }

    public static String insertLength(Object obj){
        String line = obj.toString());
        return String.format("[%d]> %s", line.length(), line));
    }

        public static void main(String[] args){
      (new ClassWithTest()).testAll());
        }
    }
<小时/>
  1. 我不知道为什么这不起作用?!!我读了 jython,但我不能使用 jython,因为我不太理解。你能帮我吗:
  2. 1-我如何从 python 运行 java 类 2-保存输出 在文本文件 3 中执行 - 将可能的错误消息保存在 另一个文本文件,然后终止。谢谢你

最佳答案

我通常使用 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")
Hello

关于java - Python3 : how to run java class file from python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227571/

相关文章:

java - Collectors.groupingBy - 收集对象的某些属性列表而不是对象本身的列表

java - 正则表达式使用 '%' 字符分割字符串但忽略 '\%'

python - 将 Mongodb 导入 CSV - 删除重复项

python - 从位于网站的图表中解析表格项时遇到问题

python-3.x - 如何绘制分类特征的 Cramer’s V 热图?

java - 在java中运行关闭 Hook 的问题

java - 运行 JAR 时出现 ClassNotFoundException,在 IntelliJ IDEA 中运行时没有错误

python - 开放式 CV 轮廓面积计算错误

python - 字符\u260e

python - 如何在多个文件之间共享Python库