Results 1 to 3 of 3
  1. #1
    paulusi is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default java.lang.ClassCastException: myclass cannot be cast to myclass

    Hi,
    I have problem with dynamically calling method.

    I have method myclass:
    public class myclass implements Serializable {
    private static final long serialVersionUID = -3756272735473013719L;
    public boolean isValid;
    public String ReturnCode;
    }
    this method I pack into separate jar file.

    I call function inside another jar file dynamically, which return myclass as parameter:
    public myclass validate(String _inputValue)
    {
    myclass cls = new myclass();
    cls.isValid = true;
    cls.ReturnCode = "return code";
    return cls;
    }

    dynamically calling:

    URL jarURL = new URL("file","",pathToJar);

    URLClassLoader ucl = new URLClassLoader(new URL[]{jarURL},ClassLoader.getSystemClassLoader());
    Class<?> cl = ucl.loadClass(_javaFunctionVal.PackageName + "." + _javaFunctionVal.ClassName);
    Class<?> cl2 = Class.forName(_javaFunctionVal.PackageName + "." + _javaFunctionVal.ClassName, true, Thread.currentThread().getContextClassLoader());


    Object obj = cl.newInstance();
    Class<?>[] cls = null;
    Object[] objj = null;
    if(_javaFunctionVal.Input!=null && _javaFunctionVal.Input.size()!=0)
    {
    cls = new Class[_javaFunctionVal.Input.size()];
    objj = new Object[_javaFunctionVal.Input.size()];
    }

    for(int i=0;i<_javaFunctionVal.Input.size();i++)
    {
    cls[i] = Class.forName((IOStructureMemberType)_javaFunction Val.Input.get (i)).Type);
    if(_inputValues.containsKey(((IOStructureMemberTyp e)_javaFunctionVal.Input.get(i)).Name))
    objj[i] = _inputValues.get(((IOStructureMemberType)_javaFunc tionVal.Input.get(i)).Name);
    }


    Method md = obj.getClass().getMethod(_javaFunctionVal.Function Name, cls);

    myclass mc = (myclass)md.invoke(obj, objj);
    If I use urlclassloader md.invoke return java.lang.ClassCastException: myclass cannot be cast to myclass
    If I use class.for name I get ClassNotFoundException.

    Exist some solution to send own output type class with dynamic method calling?
    Thanks for reply.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Welcome! First off, please use [code][/code].

    Second, can I ask your motivation for trying to load a class in this manner?

  3. #3
    paulusi is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks, I try again.
    I have xml file, which describes list of components and functions too. I need to create generator of this components. So I need to read this file and to create components which can call this external functions from jar file. I have defined class which this functions have to return.

    External function: ( inside jar file)
    Java Code:
    public myclass validate(String _inputValue)
    {
       myclass cls = new myclass();
       cls.isValid = true;
       cls.ReturnCode = "return code";
       return cls;
    }
    Return class: ( this class I pack into separate jar file.)
    Java Code:
    public class myclass implements Serializable { 
       private static final long serialVersionUID = -3756272735473013719L;
       public boolean isValid;
       public string message;
       public String ReturnCode;
    }
    dynamically calling:
    Java Code:
    URL jarURL = new URL("file","",pathToJar);
    URLClassLoader ucl = new URLClassLoader(new URL[]{jarURL},ClassLoader.getSystemClassLoader());
    Class<?> cl = ucl.loadClass(_javaFunctionVal.PackageName + "." + _javaFunctionVal.ClassName);
    Class<?> cl2 = Class.forName(_javaFunctionVal.PackageName + "." + _javaFunctionVal.ClassName, true, Thread.currentThread().getContextClassLoader());
    
    
    Object obj = cl.newInstance();
    Class<?>[] cls = null;
    Object[] objj = null;
    if(_javaFunctionVal.Input!=null && _javaFunctionVal.Input.size()!=0)
    {
    cls = new Class[_javaFunctionVal.Input.size()];
    objj = new Object[_javaFunctionVal.Input.size()];
    }
    
    for(int i=0;i<_javaFunctionVal.Input.size();i++)
    {
    cls[i] = Class.forName((IOStructureMemberType)_javaFunction Val.Input.get (i)).Type);
    if(_inputValues.containsKey(((IOStructureMemberTyp e)_javaFunctionVal.Input.get(i)).Name)) 
    objj[i] = _inputValues.get(((IOStructureMemberType)_javaFunc tionVal.Input.get(i)).Name);
    }
    
    
    Method md = obj.getClass().getMethod(_javaFunctionVal.Function Name, cls);

Similar Threads

  1. define list<myclass> in xhtml
    By miq.system in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-26-2009, 03:02 PM
  2. java.lang.ClassCastException
    By gomsy123 in forum Advanced Java
    Replies: 1
    Last Post: 03-19-2009, 05:39 PM
  3. return myClass
    By aldo in forum New To Java
    Replies: 3
    Last Post: 12-21-2008, 06:22 PM
  4. java.lang.ClassCastException
    By wrwelden in forum New To Java
    Replies: 4
    Last Post: 05-22-2008, 03:43 PM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •