mardi 11 décembre 2018

running jetty web service via reflection

Hello everyone I have been trying jetty as an embedded server for my rest web service and while using it standalone the program works fine but when i try to call the main method of the class that contains the jetty server startup logic using reflection i cant get it to work.Also when i remove the jettyServer.join line from my code then i get error java.lang.NoClassDefFoundError: org/eclipse/jetty/io/ManagedSelector$Accept and if i uncomment it then i get no error but when i try to access my web service i get 404 error.

Here is the code for server startup in a class called App

public static void main(String[] args) {
        // TODO Auto-generated method stub

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
          context.setContextPath("/JettyService/Servlet1");
          StartupListener sup=new StartupListener();
          context.addEventListener(sup);

          Server jettyServer = new Server(8080);
          jettyServer.setHandler(context);

          ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
          jerseyServlet.setInitOrder(0);

          jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", JettyServiceApp.class.getCanonicalName());

          try {
           jettyServer.start();
         // jettyServer.join();
           //stopServer(jettyServer);
          // jettyServer.stop();
          } catch (Exception e) {
           e.printStackTrace();
          } 
          finally{
          // jettyServer.destroy();
          }

    }

And here is the code that calls this method via reflection

try {
            List<URL> urlsR=new ArrayList<URL>();
//contains source classes
            File file = new File("C:\\Users\\DemonKing\\Documents\\eclipseprojects\\JettyDemo\\bin\\");
//contains the depenedencies jars of jetty
            File file2 = new File("F:\\JettyJarsComplete");
            for(File child:file2.listFiles())
            {
                File resFile=new File(file2+"\\"+child.getName());
                System.out.println(resFile);
                URL url2 = resFile.toURI().toURL();
                urlsR.add(url2);
            }

            // convert the file to URL format
            URL url = file.toURI().toURL();
            urlsR.add(url); 
            //URL url2 = file2.toURI().toURL();
            //URL[] urls = new URL[] { url,url2 };
            URL[] urls = urlsR.toArray(new URL[urlsR.size()]);
            //url

            // load this folder into Class loader
            ClassLoader cl = new URLClassLoader(urls);
            Class<?> cls = cl.loadClass("org.demonking.App");
            System.out.println("web class loaders:" + cls.getClassLoader());
            Method m = cls.getDeclaredMethod("main", String[].class);
            m.invoke(null, (Object) null);

            // code added by to avoid the resource leak class loader is
            // never closed warning
            URLClassLoader urlCl = (URLClassLoader) cl;
            urlCl.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

Need Help or pointers as to what am i doing wrong and is this scenario even achievable that is starting up a rest web service via reflection from another code.

I understand why classnotfond errors occur in java but the classloader is referencing all the jars required and iam not getting error on jetty server startup as i can see it being initailised in the logs this only happens when itry to access my web service at its path





Aucun commentaire:

Enregistrer un commentaire