4 Replies Latest reply: Aug 10, 2010 9:58 AM by 843798 RSS

    dynamic class loading

    843798
      Hi all,

      Here is the snippet of code that loads a class dynamically using an absolute path to the class file:

      Class<?> opsClass = acl.loadArahantClass(encrypted,dynamicPath ,in.getClassToBeLoaded());
      Object opsInstance = opsClass.newInstance();
      Method meth = opsInstance.getClass().getMethod(in.getMethodName(), new Class[]{DataObject.class});
      DataObject outputMap = (DataObject) meth.invoke(opsInstance, new Object[]{in.getData()});

      This works fine if I run using main; however, the problem is when I call this from a web application.

      Here is the class that it loads.

      public class Tutorial4 {


      public DataObject searchServices(DataObject in)
      {

      DataObject d=new DataObject();

      d.put("highCap", BProperty.getInt(Property.SEARCH_MAX));
      d.put("lowCap", BProperty.getInt(Property.COMBO_MAX));
      ....
      }
      }

      When calling from the web application, i keep getting this error: java.lang.ClassNotFoundException: com.arahant.services.standard.webservices.dynamicWebServices.DataObject

      Any ideas? What is the differences between running using main() vs web application? The only thing is one is running inside a web server and main is not.


      here are the supporting codes:

      public final Class<?> loadArahantClass(boolean encrypted, String path, String name) throws ClassNotFoundException, Exception {
      Class<?> cls = null;
      String className = name;
      try {
      if (!encrypted) {
      className = path className;
      +}+

      byte[] classBytes = null;
      +if (encrypted) {+
      classBytes = loadClassBytes(encrypted, className);
      +} else {+
      +{color:#ff0000}*classBytes = loadClassBytesDevelopment(className);*{color}+
      +//return Class.forName(name);+
      +}+
      cls = defineClass(name, classBytes, 0, classBytes.length);
      +if (cls != null) {+
      resolveClass(cls);
      +} else {+
      throw new ClassNotFoundException(name);
      +}+
      +} catch (Exception ex) {+
      throw new Exception(ex.getMessage());
      +}+
      return cls;
      +}+

      +private final byte[] loadClassBytesDevelopment(String name) throws Exception {+
      String cname;

      cname = name.replace('.', '/') ".class";

      InputStream in = new FileInputStream(cname);
      int fileLength = in.available();

      byte[] classBytes = new byte[fileLength];
      int len = 0;
      while (len < fileLength) {
      len += in.read(classBytes, len, fileLength - len);
      }
      in.close();

      return classBytes;
      }


      Thanks,
      Kag
        • 1. Re: dynamic class loading
          843798
          please use code tags to make your code/question more readable, right now its too hard to make anything out of it
          • 2. Re: dynamic class loading
            843798
            Sorry (firs time poster)...here is a nicer version

            ======================================================================================

            Hi all,

            Here is the snippet of code that loads a class dynamically using an absolute path to the class file:

            Class<?> opsClass = acl.loadArahantClass(encrypted,dynamicPath ,in.getClassToBeLoaded());
            Object opsInstance = opsClass.newInstance();
            Method meth = opsInstance.getClass().getMethod(in.getMethodName(), new Class[]{DataObject.class});
            DataObject outputMap = (DataObject) meth.invoke(opsInstance, new Object[]{in.getData()});

            This works fine if I run using main; however, the problem is when I call this from a web application.

            Here is the class that it loads.
            public class Tutorial4  {
            
            
                 public DataObject searchServices(DataObject in)
                 {
            
                      DataObject d=new DataObject();
            
                      d.put("highCap", BProperty.getInt(Property.SEARCH_MAX));
                      d.put("lowCap", BProperty.getInt(Property.COMBO_MAX));
            
                             ......
                     }
            }
            When calling from the web application, i keep getting this error: java.lang.ClassNotFoundException: com.arahant.services.standard.webservices.dynamicWebServices.DataObject

            Any ideas? What is the differences between running using main() vs web application? The only thing is one is running inside a web server and main is not.


            here are the supporting codes:

                 
            public final Class<?> loadArahantClass(boolean encrypted, String path, String name) throws ClassNotFoundException, Exception {
                      Class<?> cls = null;
                      String className = name;
                      try {
                           if (!encrypted) {
                                className = path + className;
                           }
            
                           byte[] classBytes = null;
                           if (encrypted) {
                                classBytes = loadClassBytes(encrypted, className);
                           } else {
                                classBytes = loadClassBytesDevelopment(className);
                                //return Class.forName(name);
                           }
                           cls = defineClass(name, classBytes, 0, classBytes.length);
                           if (cls != null) {
                                resolveClass(cls);
                           } else {
                                throw new ClassNotFoundException(name);
                           }
                      } catch (Exception ex) {
                           throw new Exception(ex.getMessage());
                      }
                      return cls;
                 }
            
                 private final byte[] loadClassBytesDevelopment(String name) throws Exception {
                      String cname;
            
                      cname = name.replace('.', '/') + ".class";
            
                      InputStream in = new FileInputStream(cname);
                      int fileLength = in.available();
            
                      byte[] classBytes = new byte[fileLength];
                      int len = 0;
                      while (len < fileLength) {
                           len += in.read(classBytes, len, fileLength - len);
                      }
                      in.close();
            
                      return classBytes;
                 }
            Thanks,
            Kag

            Edited by: kagster on Aug 9, 2010 8:22 AM

            Edited by: kagster on Aug 9, 2010 8:32 AM
            • 3. Re: dynamic class loading
              791266
              kagster wrote:
              When calling from the web application, i keep getting this error: java.lang.ClassNotFoundException: com.arahant.services.standard.webservices.dynamicWebServices.DataObject

              Any ideas? What is the differences between running using main() vs web application? The only thing is one is running inside a web server and main is not.
              What do you mean by web application? Are you doing that server side in a web application, or do you mean that you are trying to do that on the clients (of e.g an applet)
              • 4. Re: dynamic class loading
                843798
                Problem solved. Added to the custom classloader
                public MyClassLoader(){
                          super(MyClassLoader.class.getClassLoader());
                     }