1

I am trying to load a class dynamically using loadClass method of java ClassLoader in NetBeans IDE. When I run the following standalone program it gets executed properly and prints the output as expected:

public class StandaloneClass {

    public static void main(String[] args) {
        try {

            String directory = "C:\\Workspace\\ProjectMeteor\\src\\java\\com\\meteor\\loader";

            File file = new File(directory);
            URL url = file.toURI().toURL();
            URL[] urls = new URL[]{url};

            ClassLoader cl = new URLClassLoader(urls);

            Class cls = cl.loadClass("com.meteor.loader.ClassToBeLoaded");
            Object o = cls.newInstance();

            System.out.println("Class Loaded: " + o.getClass().toString());

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

}

Output:- Class Loaded: class com.meteor.loader.ClassToBeLoaded

But when I run the same piece of code (in a web application) its throwing ClassNotFoundException.

@Path("/test")
public class DynamicWebClass {

    @GET
    @Path("/getFileExplorerNodes")
    @Produces(MediaType.TEXT_PLAIN)
    public String getFileExplorerNodes() {
        try {

            String directory = "C:\\Workspace\\ProjectMeteor\\src\\java\\com\\meteor\\loader";

            File file = new File(directory);
            URL url = file.toURI().toURL();
            URL[] urls = new URL[]{url};

            ClassLoader cl = new URLClassLoader(urls);

            Class cls = cl.loadClass("com.meteor.loader.ClassToBeLoaded");
            Object o = cls.newInstance();

            return o.getClass().toString();

        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

}

Output:-
java.lang.ClassNotFoundException: com.meteor.loader.ClassToBeLoaded

Why is it throwing exception in web application when it is working fine in standalone program? How to fix this issue and make it run properly in a web application as well?

3
  • 1
    Could you please check that the class is packed in the war Commented Oct 20, 2014 at 12:13
  • Yes. The class is there in the war. And the war is present under dist folder of the project. Commented Oct 20, 2014 at 12:28
  • 2
    But you are pointing to some other directory in C:/. Provide a relative url Commented Oct 20, 2014 at 12:34

1 Answer 1

1

Looks like problem with the URL provided to the Class loader. Please provide a relative URL

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the information. Especially for pointing my url to the war. Alternatively, we can replace URLClassLoader with the default one. ClassLoader cl = DynamicWebClass.class.getClassLoader();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.