导入的包运行后Class not found ?
Class not found com.imooc.reflect.test.MethodTest
java.lang.ClassNotFoundException: com.imooc.reflect.test.MethodTest
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:773)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:502)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
正在回答
同学你好,运行贴出代码,在测试类中demo2与demo3中Address对应的引入包名不正确,例如:
如上修改后,可以正确的运行。并没有报出Class not found com.imooc.reflect.test.MethodTest 没有找到MethodTest这个类的错误,同学可以修改如上的内容后,清理缓存,重启eclipse,再来运行一下。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
package com.reflect; public class Address { private String addressId; private String addressName; private String addressDetails; private String addressTel; public Address() { } public Address(String addressId, String addressName, String addressDetails, String addressTel) { super(); this.addressId = addressId; this.addressName = addressName; this.addressDetails = addressDetails; this.addressTel = addressTel; } public String getAddressId() { return addressId; } public void setAddressId(String addressId) { this.addressId = addressId; } public String getAddressName() { return addressName; } public void setAddressName(String addressName) { this.addressName = addressName; } public String getAddressDetails() { return addressDetails; } public void setAddressDetails(String addressDetails) { this.addressDetails = addressDetails; } public String getAddressTel() { return addressTel; } public void setAddressTel(String addressTel) { this.addressTel = addressTel; } @Override public String toString() { return "Address [addressId=" + addressId + ", addressName=" + addressName + ", addressDetails=" + addressDetails + ", addressTel=" + addressTel + "]"; } public void display() { System.out.println("这是一个地址"); } private void info() { System.out.println("我是私有方法"); } private void equalsAddress(String name) { if(name.equals(this.addressName)) { System.out.println("相等"); }else { System.out.println("不相等"); } } } package com.reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; import org.junit.Test; public class addressTest { @Test /** * 通过Method调用display()方法 */ public void demo1() throws Exception{ Class class1 = Class.forName("com.reflect.Address"); Address address = (Address) class1.newInstance(); Method method = class1.getMethod("display", null); method.invoke(address, null); } @Test /** * 通过Method调用info()方法 */ public void demo2() throws Exception{ Class class2 = Class.forName("com.imooc.address.Address"); Address address = (Address) class2.newInstance(); Method method = class2.getDeclaredMethod("info", null); method.setAccessible(true); method.invoke(address, null); } @Test /** * 通过Method调用equalsAddress()方法 */ public void demo3() throws Exception{ Class class3 = Class.forName("com.imooc.address.Address"); Address address = (Address) class3.newInstance(); Method method = class3.getDeclaredMethod("equalsAddress", String.class); method.setAccessible(true); Field field = class3.getDeclaredField("addressName"); field.setAccessible(true); field.set(address, "北京"); method.invoke(address, "北京"); } }
老师帮我看下这段代码
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星