|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是Java代码:
- package com.test;
- import java.io.*;
- import java.util.*;
- class EmpManage {
- private ArrayList al = null;
- public EmpManage() {
- al = new ArrayList();
- }
- public void addEmp(Employer emp) {
- al.add(emp);
- }
- public void showEmpInfo(String no) {
- for (int i = 0; i < al.size(); i++) {
- Employer temp = (Employer) al.get(i);
- if (temp.getNo().equals(no)) {
- System.out.println("SDF" + temp.getNo() + "似睡非睡" + temp.getName() + "是否三?" + temp.getSalary());
- break;
- }
- }
- }
- public void updateSal(String empNo, float newsalary) {
- for (int i = 0; i < al.size(); i++) {
- Employer temp = (Employer) al.get(i);
- if (temp.getNo().equals(empNo)) {
- temp.setSalary(newsalary);
- break;
- }
- }
- }
-
- public void delEmp(String empNo) {
- Employer temp = null;
-
- for (int i = 0; i < al.size(); i++) {
- temp = (Employer) al.get(i);
- if (temp.getNo().equals(empNo)) {
- break;
- }
- }
- al.remove((Object) temp);
- }
- }
- @SuppressWarnings("unchecked")
- class Employer {
- private String name;
- private float salary;
- private String no;
复制代码
java主函数的代码
- package com.test;
- public class Num {
- public static void main(String[] args) {
- Employer em = new Employer("wjc",12,"no123");
- System.out.println("当前的类"+em.getClass());
- System.out.println(em.getName()+em.getNo()+em.getSalary());
- }
- }
复制代码
python调用代码:
- if __name__ == "__main__":
- import math
- import jpype
- import os
- jvmPath = r"D:\Program Files\Java\jdk1.8.0_192\jre\bin\server\jvm.dll"
- # java jar位置,被调用的jar
- jarpath = os.path.join(os.path.abspath("."), r"E:\gitee\Java\java中数组&集合类&泛型&排序\src\com\test\test.jar")
- # 启动jvm,如果已经启动了,再次启动会报错,所以try except一下
- # 其中参数convertStrings设置为false,意思是java的string不转换为python的string类型。不设置为false的话,会报警warning:
- # Deprecated: convertStrings was not specified when starting the JVM. The default
- # behavior in JPype will be False starting in JPype 0.8. The recommended setting
- # for new code is convertStrings=False. The legacy value of True was assumed for
- # this session. If you are a user of an application that reported this warning,
- # please file a ticket with the developer.
- try:
- jpype.startJVM(jvmPath,
- "-ea",
- "-Djava.class.path=%s"%(jarpath))
- except:
- pass
- # ②、加载java类(参数是java的长类名)
- javaClass = jpype.JClass('com.test.Employer')
- jc = javaClass("wjc", 144, "No123")
- print(jc.getName(), jc.getNo(), jc.getSalary())
- 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
哪位大佬帮忙看看,怎么解决的
|
|