小小小菜菜菜 发表于 2023-10-26 14:10:32

python调用java项目时,报错找不到工具类

本帖最后由 小小小菜菜菜 于 2023-10-26 14:11 编辑

公司有个 java 的 spring 项目,使用 maven 打成 jar 包,使用 python 调用加载的时候提示找不到类,有过调用 spring 项目经验的大佬给看下什么问题?
此时当调用到 CryptUtil = jpype.JClass("com.finance.micro.api.util.CryptUtil") 这个加载类的时候就报错,


mport jpype
import os,json

path = r"D:\xx\xx\wjctest\wjctest.jar"
jar_path = os.path.join(os.path.abspath("."), path)

# 获取jvm.dll的文件路径
jvmPath = jpype.getDefaultJVMPath()
print("获取的jvm路径", jvmPath)

# 启动jvm
jpype.startJVM(jvmPath, "-Dfile.encoding=UTF8", "-ea", "-Djava.class.path=%s" % jar_path)
print("看到了")

# 加载java类(参数是java的长类名)
CryptUtil = jpype.JClass("com.finance.micro.api.util.CryptUtil")
ChannelLoginDTO = jpype.JClass("com.finance.micro.dto.ChannelLoginDTO")
privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEWcZjtjUF";
publicKey = "MIGfMA0GCSqGSIb3DQEBAQUJ1QIDAQAB";

instance = ChannelLoginDTO()
instance.setChannelCode("C20220928180834695958");
instance.setCustomerName("123");
instance.setPhone("18511552233");
instance.setCertNo("612823199203631536");

params = CryptUtil.buildRequest(json.loads(x), privateKey, publicKey)
print("打印的参数",params)

# 关闭jvm
jpype.shutdownJVM()
具体报错如下:
Traceback (most recent call last):
File "D:/microFinanceProject/qa-test/test.py", line 16, in <module>
    CryptUtil = jpype.JClass("com.finance.micro.api.util.CryptUtil")
File "D:\Program Files\Anaconda3\lib\site-packages\jpype\_jclass.py", line 99, in __new__
    return _jpype._getClass(jc)
TypeError: Class com.finance.micro.api.util.CryptUtil is not found

isdkz 发表于 2023-10-26 14:11:33

问题的根本原因是Python无法找到Java类com.finance.micro.api.util.CryptUtil。这可能是由于以下几个原因导致的:

1. jar包路径错误:请确保提供的jar包路径是正确的,可以使用绝对路径或相对路径。可以使用os.path.abspath(".")来获取当前工作目录的绝对路径。

2. 缺少依赖:可能需要将项目的所有依赖项(包括jar文件)都添加到Python的classpath中。请确保所有依赖项都在-Djava.class.path参数中正确设置。

3. JVM路径错误:请确保jvm.dll文件的路径是正确的。可以使用jpype.getDefaultJVMPath()来获取默认的JVM路径。

解决方法如下:

1. 确保提供的jar包路径是正确的,可以使用绝对路径或相对路径。可以使用os.path.abspath(".")来获取当前工作目录的绝对路径。

2. 确保所有依赖项都在-Djava.class.path参数中正确设置。可以使用os.path.join()函数将jar包路径与其他依赖项路径拼接在一起。

3. 确保jvm.dll文件的路径是正确的。可以使用jpype.getDefaultJVMPath()来获取默认的JVM路径。

以下是修复后的代码示例:

import jpype
import os
import json

jar_path = r"D:\xx\xx\wjctest\wjctest.jar"
abs_jar_path = os.path.abspath(jar_path)

# 获取jvm.dll的文件路径
jvmPath = jpype.getDefaultJVMPath()
print("获取的jvm路径", jvmPath)

# 启动jvm
jpype.startJVM(jvmPath, "-Dfile.encoding=UTF8", "-ea", "-Djava.class.path=%s" % abs_jar_path)
print("看到了")

# 加载java类(参数是java的长类名)
CryptUtil = jpype.JClass("com.finance.micro.api.util.CryptUtil")
ChannelLoginDTO = jpype.JClass("com.finance.micro.dto.ChannelLoginDTO")
privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEWcZjtjUF";
publicKey = "MIGfMA0GCSqGSIb3DQEBAQUJ1QIDAQAB";

instance = ChannelLoginDTO()
instance.setChannelCode("C20220928180834695958")
instance.setCustomerName("123")
instance.setPhone("18511552233")
instance.setCertNo("612823199203631536")

params = CryptUtil.buildRequest(json.loads(x), privateKey, publicKey)
print("打印的参数", params)

# 关闭jvm
jpype.shutdownJVM()


通过以上修改,应该能够解决找不到工具类的问题。如果问题仍然存在,请检查jar包路径、依赖项和JVM路径是否正确,并确保工具类在jar包中存在。

陶远航 发表于 2023-10-26 14:11:37

The error message suggests that the Python script is unable to find the class com.finance.micro.api.util.CryptUtil in the Java project. Here are a few things you can check:

1. Ensure that the JAR file containing the Java project (wjctest.jar) is located in the correct path D:\xx\xx\wjctest\.
2. Confirm that the class com.finance.micro.api.util.CryptUtil is present in the JAR file. You can inspect the contents of the JAR file using a tool like jar tf wjctest.jar to verify the class's location.
3. Check if there are any dependencies or additional JAR files required by the Java project. Make sure all necessary dependencies are included and accessible.
4. Verify that the class path specified in the Python script (-Djava.class.path=%s" % jar_path) correctly points to the JAR file containing the required class. Double-check the value of jar_path to ensure it is the correct path.

By reviewing these points, you should be able to identify and resolve the issue with finding the Java class in your Python script.
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
页: [1]
查看完整版本: python调用java项目时,报错找不到工具类