簡體   English   中英

Python3:如何從python運行Java類文件

[英]Python3: how to run java class file from python

我從以下鏈接閱讀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導入為jp ModuleNotFoundError中:沒有名為“ jpype”的模塊

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

  • 我必須指出我的java類在我的python路徑中,而我的java類是:
  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM