Servlet无法动态读取Class

板栗
  • 1
新手上路,请多包涵

题目描述

最近我想做一个Servlet的demo。这个demo中用户可以将自己的class文件上传到我的服务器中,然后我用我的Servlet读取用户上传的class中的方法。但我卡在Serlvet无法动态读取类这个环节。

代码

代码1 showMsg (Servlet)

public class showMsg extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showMsg() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            out.println(ReadClass.getProperty());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

代码2 ReadClass (java)

public class ReadClass {
    public static String getProperty() throws ClassNotFoundException {
        try {
            URL url = new URL(null, "file:C:/TestClass/");
            ClassLoader classLoader = new URLClassLoader(new URL[]{url}, ReadClass.class.getClassLoader());
            Class<?> thisClass = classLoader.loadClass("add");
            return thisClass.getMethods()[0].getName();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
        /*
        File myFolder = new File("file:C:/TestClass/");
        URLClassLoader classLoader = null;
        try {
            classLoader = new URLClassLoader(new URL[]{myFolder.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        
        System.out.println(classLoader);
        //获取对象类型    包名.类名   ?代表任意类型对象
        Class<?> classType=Class.forName("add", true, classLoader);

        Object obj = null;
        try {
            obj = classType.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //获取对象中的所有方法
        Method[] methods = classType.getDeclaredMethods();
        try {
            return methods[0].invoke(obj).toString();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return "";
        */
    }
}

补充说明

我要读取的class的路径是“C:/TestClass/add.class”

相关问题

但当我把相同的代码复制到普通的java项目中时,这个问题却没有出现,程序能完美的运行。我查了很多资料,最相近的问题的解决方案是让我给ClassLoader设置一个家长。如你们所见,我的代码中有设置,但是仍旧报错,请问大家知道原因吗

相似问题的链接

https://stackoverflow.com/que...

回复
阅读 242
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏