5 Replies Latest reply on Jun 6, 2014 6:41 AM by hai_feng

    about classloader of wildfly

    hai_feng

      Hi everyone

      I have one question want to ask you.

       

      I write a servlet as following:

       

      @WebServlet("/TestPath")

      public class TestPath extends HttpServlet {

        private static final long serialVersionUID = 1L;

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // TODO Auto-generated method stub

          ClassLoader parent = TestPath.class.getClassLoader();

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

           if (parent instanceof URLClassLoader) {

                 System.out.println("ClassLoader:" + parent.getClass().getName());

                 System.out.println("ClassLoader1:" + parent1.getClass().getName());

               }

        }

      I deploy it on wildfly,and visit it,but the console print nothing.

      when i deploy it on tomcat,visit it,and console print as follow:

       

      ClassLoader:org.apache.catalina.loader.WebappClassLoader

      ClassLoader1:org.apache.catalina.loader.WebappClassLoader

       

      What confuse me is why  TestPath.class.getClassLoader() and Thread.currentThread().getContextClassLoader() not instanceof URLClassLoader but on the tomcat they are all instanceof URLClassLoader.What different about classloader between wildfly and tomcat?or if I can change wildfly's classloader to sovle it?


      Please help!

      Thanks!

        • 1. Re: about classloader of wildfly
          ctomc

          WildFly doesn't use URLClassLoader anymore, last version that used it was AS6.

           

          Since AS7 we use ModuleClassLoader which is implemented by Jboss modules.

           

          see more about it:

          https://docs.jboss.org/author/display/MODULES/Home

          https://docs.jboss.org/author/display/WFLY8/Class+Loading+in+WildFly

          • 2. Re: about classloader of wildfly
            hai_feng

            Hi,Tomaz

            Thanks your reply,But when i scan the ducument(Home - JBoss Modules - Project Documentation Editor),there is no content about Modules API.The second ducument is not overall about ModuleClassLoader.

            I want to know more about ModuleClassLoader and other classLoader about WildFly.My project use Tomcat past,now we want to move to wildfly,but when we visit the project on wildfly,it tells us can't use ModuleClassLoader.as you said as follow:

            "

            WildFly doesn't use URLClassLoader anymore, last version that used it was AS6.

             

            Since AS7 we use ModuleClassLoader which is implemented by Jboss modules.

            "

            So i know the reason about this problem,and i write a demo to simulate the function in our project.

            Example as following:

            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              // TODO Auto-generated method stub

                 ClassLoader parent = TestPath.class.getClassLoader();

                 URLClassLoader uL;

                 URL[] path;

                  int i;

                   if (parent instanceof URLClassLoader) {

                       System.out.println("ClassLoader:" + parent.getClass().getName());

                     uL = (URLClassLoader)parent;

                     path = uL.getURLs();

                     if (path != null) {

                       for (i = 0; i < path.length; ++i) {

                          System.out.println(path[i].getPath());

                       }

                   }

                 }

              }

            use Tomcat,the console print:

            ClassLoader:org.apache.catalina.loader.WebappClassLoader

            /S:/springToolSuite/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/testpath/WEB-INF/classes/

            /S:/springToolSuite/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/testpath/WEB-INF/lib/jboss-modules.jar

             

            Because Wildfly doesn't use URLClassLoader anymore,so above java code make invalid. Now,I want to know what api of ModuleClassLoader can i use to make the same function on wildfly?

             

            please help!

            Thanks!



            • 3. Re: about classloader of wildfly
              ctomc

              Wind Wild wrote:

               

              Because Wildfly doesn't use URLClassLoader anymore,so above java code make invalid. Now,I want to know what api of ModuleClassLoader can i use to make the same function on wildfly?



               

              Nothing really special here, just use standard java.lang.ClassLoader api, no need to cast it so impl version and call some special methods on it...

              • 4. Re: about classloader of wildfly
                hai_feng

                Hi,Tomaz

                Firstly,I know what you mean,but i can't find any api of ClassLoader make the same function to URLClassLoader,I just want to get the directory of war,what can i do?

                Secondly,i write a demo as following:

                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                  // TODO Auto-generated method stub

                     ClassLoader parent = TestPath.class.getClassLoader();

                     ClassLoader uL;

                     URL path;

                     int i;

                       if (parent instanceof ClassLoader) {

                           System.out.println("ClassLoader:" + parent.getClass().getName());

                         uL = (ClassLoader)parent;

                         path = uL.getResource("/com/classloader/test");

                              System.out.println(path.getPath());

                     } 

                  }

                the ""/com/classloader/test" is this class's package.

                Now,when i deploy it to wildfly,the console print(os:windows)

                3.jpg

                when i deploy it to wildfly,the console print(os:linux)

                4.jpg

                when i deploy it to tomcat,the console print:

                2.jpg

                we can see that it's normal on tomcat,but it seem to be off normal on wildfly.What confuse me is why the directory of /com/classloader/test under the JBOSS_HOME/bin (windows wildfly)?the directory of /com/classloader/test under the linux root directory (linux wildfly).

                According to my understanding,is it should under JBOSS_HOME\standalone\tmp\vfs\deployment ?

                 

                Please Help!

                Thanks!

                • 5. Re: about classloader of wildfly
                  hai_feng

                  Hi Tomaz:

                  about second question,i have solve it,as follow:

                  public String getRealFilePath(String aFilePath) throws Exception {

                       org.jboss.vfs.VirtualFile vFile = org.jboss.vfs.VFS.getChild(aFilePath);

                       URI fileNameDecodedTmp = org.jboss.vfs.VFSUtils.getPhysicalURI(vFile); 

                       path = fileNameDecodedTmp.getPath();

                       System.out.println(path);

                       return path;

                       }

                  So at runtime I just need to call getRealFilePath() with the original question values, and problem solved

                  but i want to the first question's answer,please tell me,Thanks!