鱼C论坛

 找回密码
 立即注册
查看: 395|回复: 2

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

[复制链接]
回帖奖励 200 鱼币 回复本帖可获得 2 鱼币奖励! 每人限 1 次(中奖概率 50%)
发表于 2023-10-26 14:10:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

微信图片_20231026140241.png
  1. mport jpype
  2. import os,json

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

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

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

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

  16. instance = ChannelLoginDTO()
  17. instance.setChannelCode("C20220928180834695958");
  18. instance.setCustomerName("123");
  19. instance.setPhone("18511552233");
  20. instance.setCertNo("612823199203631536");

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

  23. # 关闭jvm
  24. 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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路径。

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

  1. import jpype
  2. import os
  3. import json

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

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

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

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

  17. instance = ChannelLoginDTO()
  18. instance.setChannelCode("C20220928180834695958")
  19. instance.setCustomerName("123")
  20. instance.setPhone("18511552233")
  21. instance.setCertNo("612823199203631536")

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

  24. # 关闭jvm
  25. jpype.shutdownJVM()
复制代码


通过以上修改,应该能够解决找不到工具类的问题。如果问题仍然存在,请检查jar包路径、依赖项和JVM路径是否正确,并确保工具类在jar包中存在。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-29 19:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表