3 Replies Latest reply on Oct 25, 2016 11:40 AM by Victor Hugo

    Wildfly read project libs

    Victor Hugo Newbie

      Hi,

       

      I'm developing an application where I will compile code in memory and then execute it. To compile this code I need some libs which I added to my classpath, for example, JUnit.

       

      When developing using Eclipse IDE, everything worked but when I deployed the war file to the server using stadalone.xml, it started to search the libs inside "/bin/content". I search for a solution but every thread that I found was looking for a specific file inside classes/ folder.

      Also, when I deploy the war file using domain.xml configuration It doesn't even find the classloader.

       

      How could I take my classpath libs using wildfly classloader?

       

      I'm using wildfly 9.0.2.

      Using tomcat:

      URLClassLoader urlClassLoader = (URLClassLoader) parent;
              for (URL url : urlClassLoader.getURLs()) {
                  sb.append(url.getFile()).append(File.pathSeparator);
                  System.out.println("ADD Lib: " + url);
              }
      

       

      Using wildfly:

      ClassLoader parent = Thread.currentThread().getContextClassLoader();
      Enumeration<URL> resources = parent.getResources("/");     
              System.out.println(InMemoryCompiler.class.getClassLoader().getResources("/"));        
              while (resources.hasMoreElements()) {
                  URL url = resources.nextElement();
                  sb.append(url.getFile()).append(File.pathSeparator);
                  System.out.println("ADD Lib: " + url);
              }
      

       

      Thank you.

        • 1. Re: Wildfly read project libs
          Tomaz Cerar Master

          What are you trying to do?

          WildFly doesn't use URL classloader but rather Modular classloader.

          and as such doesn't really have paths for the modules but module names.

           

          So better question is what are you trying to achieve as there could probably be a different way.

          • 2. Re: Wildfly read project libs
            Victor Hugo Newbie

            Thanks for you answer Tomaz.

             

            What I need to do is take a java file uploaded by a website, compile and execute.

            List<String> options = new ArrayList<String>();
            
                    options.add("-classpath");
                    StringBuilder sb = new StringBuilder();
                    Enumeration<URL> resources = parent.getResources("/");
                    while (resources.hasMoreElements()) {
                        URL url = resources.nextElement();
                        sb.append(url.getFile()).append(File.pathSeparator);
                        System.out.println("ADD Lib: " + url);
                    }
            
                    options.add(sb.toString());
            
                    // execute the compiler
                    Boolean call = javac.getTask(null, fileManager, null, options, null, compilationUnits).call();
            

             

            I need to compile this code using my classpath (which has the libraries needed), otherwise I'll get classnotfound when trying to compile.

            • 3. Re: Wildfly read project libs
              Victor Hugo Newbie

              Any ideas how could I do something similar?
              Using wildlfy from Eclipse IDE I can get the URL to my libs and compile the code successfully. The problem happens only when I deploy using wildfly as stadonalone or domain... It also works with tomcat 8.

               

              Don't know how to keep up...