鱼C论坛

 找回密码
 立即注册
查看: 1853|回复: 3

谁知道python代码调用java代码总是不成功为什么

[复制链接]
发表于 2022-5-26 15:49:01 | 显示全部楼层 |阅读模式

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

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

x
以下是Java代码:
  1. package com.test;

  2. import java.io.*;
  3. import java.util.*;


  4. class EmpManage {
  5.         private ArrayList al = null;

  6.         public EmpManage() {
  7.                 al = new ArrayList();
  8.         }

  9.         public void addEmp(Employer emp) {
  10.                 al.add(emp);
  11.         }

  12.         public void showEmpInfo(String no) {
  13.                 for (int i = 0; i < al.size(); i++) {
  14.                         Employer temp = (Employer) al.get(i);
  15.                         if (temp.getNo().equals(no)) {
  16.                                 System.out.println("SDF" + temp.getNo() + "似睡非睡" + temp.getName() + "是否三?" + temp.getSalary());
  17.                                 break;
  18.                         }
  19.                 }
  20.         }


  21.         public void updateSal(String empNo, float newsalary) {
  22.                 for (int i = 0; i < al.size(); i++) {
  23.                         Employer temp = (Employer) al.get(i);
  24.                         if (temp.getNo().equals(empNo)) {
  25.                                 temp.setSalary(newsalary);
  26.                                 break;
  27.                         }
  28.                 }
  29.         }

  30.        
  31.         public void delEmp(String empNo) {
  32.                 Employer temp = null;
  33.                
  34.                 for (int i = 0; i < al.size(); i++) {
  35.                         temp = (Employer) al.get(i);
  36.                         if (temp.getNo().equals(empNo)) {
  37.                                 break;
  38.                         }
  39.                 }
  40.                 al.remove((Object) temp);
  41.         }
  42. }
  43. @SuppressWarnings("unchecked")
  44. class Employer {
  45.         private String name;
  46.         private float salary;

  47.         private String no;
复制代码

java主函数的代码
  1. package com.test;

  2. public class Num {

  3.         public static void main(String[] args) {
  4.                 Employer em = new Employer("wjc",12,"no123");
  5.                 System.out.println("当前的类"+em.getClass());
  6.                 System.out.println(em.getName()+em.getNo()+em.getSalary());
  7.         }

  8. }
复制代码


python调用代码:
  1. if __name__ == "__main__":
  2.     import math
  3.     import jpype
  4.     import os
  5.     jvmPath = r"D:\Program Files\Java\jdk1.8.0_192\jre\bin\server\jvm.dll"
  6.     # java jar位置,被调用的jar
  7.     jarpath = os.path.join(os.path.abspath("."), r"E:\gitee\Java\java中数组&集合类&泛型&排序\src\com\test\test.jar")
  8.     # 启动jvm,如果已经启动了,再次启动会报错,所以try except一下
  9.     # 其中参数convertStrings设置为false,意思是java的string不转换为python的string类型。不设置为false的话,会报警warning:
  10.     # Deprecated: convertStrings was not specified when starting the JVM. The default
  11.     # behavior in JPype will be False starting in JPype 0.8. The recommended setting
  12.     # for new code is convertStrings=False. The legacy value of True was assumed for
  13.     # this session. If you are a user of an application that reported this warning,
  14.     # please file a ticket with the developer.
  15.     try:
  16.         jpype.startJVM(jvmPath,
  17.                        "-ea",
  18.                        "-Djava.class.path=%s"%(jarpath))
  19.     except:
  20.         pass
  21.     # ②、加载java类(参数是java的长类名)
  22.     javaClass = jpype.JClass('com.test.Employer')
  23.     jc = javaClass("wjc", 144, "No123")
  24.     print(jc.getName(), jc.getNo(), jc.getSalary())
  25.     jpype.shutdownJVM()
复制代码

执行python代码时会出现如下的报错
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/gitee/Python/扩展练习/game.py", line 76, in <module>
    javaClass = jpype.JClass('com.test.Employer')
  File "E:\gitee\Python\venv\lib\site-packages\jpype\_jclass.py", line 99, in __new__
    return _jpype._getClass(jc)
TypeError: Class com.test.Employer is not found

哪位大佬帮忙看看,怎么解决的

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-26 15:54:27 | 显示全部楼层
它说类找不到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-31 11:11:22 From FishC Mobile | 显示全部楼层
当初约定 发表于 2022-5-26 15:54
它说类找不到

就是这个问题,我也是提示类找不到,所以来论坛请教下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-31 18:18:41 | 显示全部楼层
访问性修饰符的问题吧?没学过 java 不清楚,是不是应该在类前面加 public 修饰符?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 05:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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