首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于加载class文件,无法找到文件有关问题

2012-12-17 
关于加载class文件,无法找到文件问题代码目的:利用java中classLoader,加载一个class文件进内存成class实例

关于加载class文件,无法找到文件问题
代码目的:利用java中classLoader,加载一个class文件进内存成class实例。

package code.loader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class ClassLoaderDemo {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        try {
        
            // 测试路径
            String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
            String classPath2 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
            // 测试类
            String className1 = "Tesst2";
            String className2 = "Tesst2";
            // 创建个URL
            URL url1 = new URL(classPath1);
            // 建立ClassLoader
            ClassLoader loader1 = new URLClassLoader(new URL[] {url1});
            System.out.println(className1);
            // 载入类
            Class c1 = loader1.loadClass(className1);
            System.out.println(c1);
       
            URL url2 = new URL(classPath2);
            ClassLoader loader2 = new URLClassLoader(new URL[] {url2});
            Class c2 = loader2.loadClass(className2);
            System.out.println(c2);
       
            System.out.println("是否相同?" + (c1 == c2));
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("路劲不对");
        }
        catch(MalformedURLException e) {
            System.out.println("载入路径错误");
        }
        catch(ClassNotFoundException e) {
            System.out.println("找不到指定的类");
        }
    }
}


结果是:找不到指定的类及ClassNotFoundException 的异常,是loader1.loadClass(className1);代码包的异常。
1,检查路劲是对的,否则也通不过new URLClassLoader(new URL[] {url1});这代码吧


2,检查Tesst2.class文件存在
3,问题已经非常清晰,如果实现过类似功能的朋友,可以帮我找下你以前成功的代码帮助我一下,没有代码提供的朋友,可以凭你的经验指出可能出错的地方,我愿意自己尝试你的想法,直到解决问题。先谢谢各位了。

补充:这里贴出Test2的代码:

package code.mytest;

public class Test2 {
public void sayYouILY(){
System.out.println("I love you!");
}
}

[最优解释]
粗略看了下,类名和路径名都不正确。

String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
应该是:
String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";


String className1 = "Tesst2";
应该是:
String className1 = "code.mytest.Tesst2";
[其他解释]
bin目录文件中的就是java对应的class文件(带包名);
class文件名的话就是从bin的下一级目录开始算起。
file:/E:/eclipse/workspace/OK/bin/code/mytest/Tesst2.class

[其他解释]
你有没有尝试过从CMD命令行来运行过带包名的类?
路径是要从包的根开始指定的。

其次,你指定的类名如果是 Tesst2,对于Java来说跟code.mytest.Tesst2是完全不同的两个类。
[其他解释]
引用:
粗略看了下,类名和路径名都不正确。

String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
应该是:
String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";


String className1 = "Tesst2……

我的class文件是在E:/eclipse/workspace/OK/bin/code/mytest文件夹下的,依照你的写法我改正后还是报同样的错。你看是不是这个功能本来就不能实现的呀?
[其他解释]
引用:
粗略看了下,类名和路径名都不正确。

String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
应该是:
String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";


String className1 = "Tesst2……

修改如下:
 // 测试路径
            String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";
            String classPath2 = "file:/E:/eclipse/workspace/OK/bin/";
            // 测试类
            String className1 = "code/mytest/Tesst2";
            String className2 = "code/mytest/Tesst2";
可是依然不可行呀
[其他解释]
一定是可实现的,我很久以前就做过。

只不过要搭建下环境,然后测试代码啥的,有些耗费时间,所以没有直接测试,先指出我最怀疑的地方。
[其他解释]
引用:
你有没有尝试过从CMD命令行来运行过带包名的类?
路径是要从包的根开始指定的。

其次,你指定的类名如果是 Tesst2,对于Java来说跟code.mytest.Tesst2是完全不同的两个类。

不好意思 我看了你指出的错误点,我没修改正确,应该你说的是对的。
[其他解释]
引用:
一定是可实现的,我很久以前就做过。

只不过要搭建下环境,然后测试代码啥的,有些耗费时间,所以没有直接测试,先指出我最怀疑的地方。

嗯 谢谢 是我修改时太急 打错了 呵呵 你第一个回复是好用的 非常感谢。
[其他解释]
引用:
bin目录文件中的就是java对应的class文件(带包名);


class文件名的话就是从bin的下一级目录开始算起。
file:/E:/eclipse/workspace/OK/bin/code/mytest/Tesst2.class


是的 你说的是对的 现在修改完毕如下,感谢
[其他解释]
引用:
引用:粗略看了下,类名和路径名都不正确。

String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
应该是:
String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";


String cl……

// 测试类
String className1 = "code.mytest.Tesst2";
String className2 = "code.mytest.Tesst2";
[其他解释]
好吧,我也测试了下,也发出来给你参考吧。。。


package reflect;

import java.lang.reflect.*;
import java.net.*;

public class ReflectCreateClass {

    public static void main(String[] args) throws Exception {
        // ClassPath
        URL classPath = ReflectCreateClass.class.getResource("/");
        System.out.println("路径:" + classPath);

        // ClassLoader
        ClassLoader loader = new URLClassLoader(new URL[] {classPath});
        String className = "reflect.test.Test2"; // 我用的包跟你不一样,内容完全一样的
        
        // Get Class Definition for Reflect
        Class clazz = loader.loadClass(className);
        System.out.println(clazz);
        Method method = clazz.getDeclaredMethod("sayYouILY");
        System.out.println(method);
        
        // Create Object & Invoke Method
        Object obj = clazz.newInstance();
        method.invoke(obj);
    }

}

热点排行