5 Replies Latest reply on Apr 5, 2014 3:55 AM by aanshu_2302

    Loading class files at runtime

    aanshu_2302

      Hi,

      I have an EJB based enterprise app and am using JBoss 7.

      I have an initializer class where in I read all my entitites using ClassLoader and store the data in a Hashmap. I want to store all my metadata for the tables in the hashmap.

       

      I am able to do it successfully on the dev server when I use Eclipse as the deployment are exploded by default by Eclipse. However, when I deploy the app in Production, the .Ear file is not exploded and the class files are not read. This is happening for a lot of other files like .properties as well.

       

      I want to know how to solve it. I am trying to read the files by getting the URL of the resource which should work ideally in all scenarios but still this issue comes.

      Can anyone let me know how to solve this.

       

      Thanks,

      Anshu

        • 1. Re: Loading class files at runtime
          wdfink

          Could you show an example how you package the application and how your code look like?

          • 2. Re: Loading class files at runtime
            aanshu_2302

            I am passing my entities packagename.

             

            private static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException {

              ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

              assert classLoader != null;

              String path = packageName.replace('.', '/');

              Enumeration<URL> resources = classLoader.getResources(path);

              List<File> dirs = new ArrayList<File>();

              while (resources.hasMoreElements()) {

              URL resource = resources.nextElement();

              dirs.add(new File(resource.getFile()));

              }

              ArrayList<Class<?>> classes = new ArrayList<Class<?>>();

              for (File directory : dirs) {

              classes.addAll(findClasses(directory, packageName));

              }

              System.out.println("Classes : " + classes);

              return classes.toArray(new Class[classes.size()]);

              }


            Relevant code is attached.

            My ear file has ejb.jar and 2 war files.

            • 3. Re: Loading class files at runtime
              wdfink

              File access will be not supported for EE applications, there might be issues as you see here.

               

              You might use the same class loader from a class inside your application, not the current contextCL.

              Also you need to consider that you might have restrictions with war files, see Class Loading in AS7

              • 4. Re: Loading class files at runtime
                aanshu_2302

                Could you tell me what all needs to be done if I have to do this.

                I have a requirement to read class metadata of all my entities and store it during server startup. I have a Singleton class for that.

                 

                Please suggest.

                • 5. Re: Loading class files at runtime
                  aanshu_2302

                  I have solved the problem by reading metadata directly from Entitymanager itself.

                  Earlier, I had wanted to avoid this as I thought maybe reading the class files directly would be better off than just reading those from EntityManager but nevertheless went ahead with Entitymanager approach.