2 Replies Latest reply on Jun 15, 2014 11:01 PM by hai_feng

    how to get project's classpath under wildfly

    hai_feng

      Hi everybody!

      Today I meet a problem what is described as follows:

      In the past,I used the follow code to get the project's classpath:

      URL resourceURL = Thread.currentThread().getContextClassLoader().getResource("");

      this function can get the classpath of project on tomcat,but can't on wildfly.

      Following is my test demo:

       

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
           URL resourceURL = Thread.currentThread().getContextClassLoader().getResource("com/classloader/test/TestPath.class");
              URL resourceURL1 = Thread.currentThread().getContextClassLoader().getResource("");
              URL resourceURL2 = Thread.currentThread().getContextClassLoader().getResource("/");
              URL resourceURL3 = TestPath.class.getResource("");
             
              System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource('com/classloader/test/TestPath.class'):");
              System.out.println(resourceURL.getPath());
              System.out.println();
              System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource(''):");
              System.out.println(resourceURL1.getPath());
              System.out.println();
              System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource('/'):");
              System.out.println(resourceURL2.getPath());
              System.out.println();
              System.out.println("Function-------TestPath.class.getResource(''):");
              System.out.println(resourceURL3.getPath());
          }
      
      

      now I deploy it on tomcat,the console print as follow:

      1.jpg

      but when i deploy it on wildfly,the console print as follow:

      2.jpg

      you can see that the function:

      URL resourceURL1 = Thread.currentThread().getContextClassLoader().getResource("");

      has different result between tomcat and wildfly,it can get project's classpath on tomcat,but it get "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/",we all know that this path isn't project's classpath.

      So i want to know what mean of "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/",why a same fuction can get different result path on different web server?

      And who can tell me how to get project's classpath(JBOSS_HOME/bin/content/testpath.war/WEB-INF/classes/) directly under wildfly?what api i can use?

      and you can see that my war is located under JBOSS_HOME/bin ,the follow methor can sovle it:


      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);
           String path = fileNameDecodedTmp.getPath();
           return path;
        }
      

       

      Please help!

      Thanks!

        • 1. Re: how to get project's classpath under wildfly
          jameslivingston

          It can do that because calling getResource() with a directory path is not defined. With many classloaders it returns the URL of the directory, but it doesn't have to. I would think passing the empty string is even less defined, and you probably should use "/" if you really want to do it.

           

          There is no properly portable way to get the path to your WEB-INF/classes directory, one major reason being that it may not even exist (if the server doesn't explode the WAR as an implementation detail). servletContext.getRealPath("WEB-INF/classes") may work, but again I don't think is portable to rely on that.

           

           

          Why do you want to know the location of your own classes directory?

          1 of 1 people found this helpful
          • 2. Re: how to get project's classpath under wildfly
            hai_feng

            Okay James,thanks your reply firstly.as you said "you probably should use "/" if you really want to do it." ,I guess you were ignore of my demo.you can see that i had writed "URL resourceURL2 = Thread.currentThread().getContextClassLoader().getResource("/");  "in my demo,but it print "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/" also.So i want to know why it gets the directory is "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/"?

            follow your second advice,I try to use "servletContext.getRealPath("WEB-INF/classes")" to get directory of classes,and it works.

             

            About your question "Why do you want to know the location of your own classes directory?",what i can tell you is that we project has this requese,and i want to use this chance to learn wildfly‘s classloader,Do you know more about that?Please help!

             

            Thanks!