vendredi 30 novembre 2018

No such method exception when using method via reflection

I have been getting my hands dirty with Java reflection and have come across a bit of a hurdle. If I try to call the following method via reflection I get a java.lang.NoSuchMethodException: org.demonking.CrossHandler.HandleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) thrown.

Here is the code for the method:

public void HandleRequest(HttpServletRequest req,HttpServletResponse resp)
{
    try{
        System.out.println("i am here");

        //RequestDispatcher view = req.getRequestDispatcher("CrossFile.jsp");
        // don't add your web-app name to the path
       // view.forward(req, resp); 
    }catch(Exception ex)
    {
        System.out.println("in exception");
    }
}

and here is the reflection code:

try {
    Method m=cls.getMethod("HandleRequest", HttpServletRequest.class,HttpServletResponse.class); // error on this line
    //Object obj=cls.newInstance();
    //m.invoke(obj,new Object[]{req,resp});
} catch (Exception ex) {
    ex.printStackTrace();
    System.out.println(ex.getCause());
}

Note that other methods called via reflection works just fine.





Aucun commentaire:

Enregistrer un commentaire