Я работаю над Java и получил требование интегрировать его с Python.
Итак, я нашел пакет JPype в Python
public class JPypeExample {
public static int addition(Integer... numbers) {
int sum = 0;
for(int i : numbers) {
sum += i;
}
return sum;
}
public int addNumbers(int a, int b, int c, int d) {
return a+b+c+d;
}
}
В Python я вызываю файл Java и выполняю егои работает нормально
import jpype
import os
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s"%os.path.abspath("."))
JPypeExample= jpype.JClass("JPypeExample")
numbers = jpype.JArray(jpype.java.lang.Integer, 1)(4)
numbers[0] = jpype.java.lang.Integer(13)
numbers[1] = jpype.java.lang.Integer(1)
numbers[2] = jpype.java.lang.Integer(20)
numbers[3] = jpype.java.lang.Integer(8)
jPypeExample = JPypeExample()
print ("The result is: %d"%jPypeExample.addNumbers(13,1,20,8))
print ("The result is: %d"%JPypeExample.addition(numbers))
jpype.shutdownJVM()
Вывод: -
The result is: 42
The result is: 42
Теперь я импортирую jar в java
import java.net.*;
import java.io.*;
import com.somejar.TimeUnit;
import com.somejar.TimeUnit1;
import com.somejar.TimeUnit2;
public class Hello {
public int Test(int number) throws Exception {
while (true){
System.out.println("Hello, World");
TimeUnit.SECONDS.sleep(number);
}
}
}
Я скомпилировал его и получил файл .class ив python ошибка выброса.
import jpype
import os
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s"%os.path.abspath("."))
JPypeExample= jpype.JClass("Hello")
jPypeExample = JPypeExample()
print ("The result is: %d"%jPypeExample.Test(15))
Мой вопрос заключается в том, как импортировать или связывать jar в JPype, чтобы он мог напрямую выполняться как метод, как указано выше.