How to load .class from working directory at runtime from jar?

Ben Palmer

Attempting to implement a plugin system for my application, the code of which extends my Plugin class to define custom functionality.

This code to load a class from within a jar, in the same directory as the .jar of the application, works fine:

if(pluginName.endsWith(".jar"))
{
    JarFile jarFile = new JarFile(pluginName);
    Enumeration jarComponents = jarFile.entries();

    while (jarComponents.hasMoreElements())
    {
        JarEntry entry = (JarEntry) jarComponents.nextElement();
        if(entry.getName().endsWith(".class"))
        {
            className = entry.getName();
        }
    }

    File file = new File(System.getProperty("user.dir")+"/"+pluginName);
    URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});

    Class newClass = Class.forName(className.replace(".class", ""), true, loader);
    newPlugin = (Plugin) newClass.newInstance();
}

Which results in the correct response of:

benpalmer$ java -jar assignment.jar test_subproject.jar
- INFO: Add new plugin: source.com

The issue is I also want to provide the user the option to specify a .class file instead, as that is realistically all that is packed into a .jar for the purposes of my plugin system.

else if(pluginName.endsWith(".class"))
{
    File file = new File(System.getProperty("user.dir")+"/"+pluginName);
    URLClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()});

    Class newClass = Class.forName(className.replace(".class", ""), true, loader);
    newPlugin = (Plugin) newClass.newInstance();
}   
... exception handling

Which results in the following:

benpalmer$ java -jar assignment.jar TestPlugin.class
SEVERE: Error: Plugin class not found. Class name: 
java.lang.ClassNotFoundException: 
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at newsfeed.model.Plugin.loadClassFromJarFile(Plugin.java:144)
    at newsfeed.controller.NFWindowController.initPlugins(NFWindowController.java:81)
    at newsfeed.controller.NewsFeed$1.run(NewsFeed.java:20)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I've tried the code above and many variations of path names/other options, but get ClassNotFoundException every time. Originally had trouble loading the jars but using the fully qualified path fixed that, now I can't load the Classes.

Any ideas? :)

Ben Palmer

Saw my question again and realised I'd forgotten to post a solution I found to my problem. I hope this helps someone in the future, as it's the only working example I ever found for my situation - a university assignment problem. I can't believe it was this simple:

byte[] classData = Files.readAllBytes(Paths.get(fileName));
Class<?> cls = defineClass(null, classData, 0, classData.length); 
return (PluginClassName)cls.newInstance();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to load unknown class from downloaded jar file during runtime?

From Dev

How to load a JAR from Assets folder at runtime

From Dev

ClassNotFoundException while trying to load class from external JAR at runtime

From Dev

Load a class from a jar

From Dev

How to load a class from a .jar Android

From Dev

How to load a class from a .jar Android

From Dev

FindClass on a Class that was loaded from a jar file at runtime

From Dev

How to add class files and folders to a jar file from a directory in Java

From Dev

Load pytorch model from different working directory

From Dev

Failing to load class definition from jar

From Dev

How to make .jar from .class

From Dev

Load a class from a jar having dependency on another jar

From Dev

Java 6 runtime not overriding class called from within .jar dependency

From Dev

Java 6 runtime not overriding class called from within .jar dependency

From Dev

Extract jar from aar for cordova plugin, Class not found error at runtime

From Dev

Load and instantiate class from jar in class search path

From Dev

How to load resources from other JAR file

From Dev

How to load Hibernate entities from external JAR

From Dev

LWJGL 3 stbi_load_from_memory not working when in jar

From Dev

How to force a dll to load from current directory

From Dev

How to load a random file from a directory

From Dev

How to load images from a directory on the computer in Python

From Dev

How to load javascript file from another directory

From Dev

How to load images from a directory on the computer in Python

From Dev

How to load all the images from one Directory

From Dev

Cannot load main class from JAR file in Spark Submit

From Dev

unable to load java class from jar file file

From Dev

Python - how do i load my custom class methods from a root directory?

From Dev

How to load a library from JAR file loaded from browser?

Related Related

  1. 1

    How to load unknown class from downloaded jar file during runtime?

  2. 2

    How to load a JAR from Assets folder at runtime

  3. 3

    ClassNotFoundException while trying to load class from external JAR at runtime

  4. 4

    Load a class from a jar

  5. 5

    How to load a class from a .jar Android

  6. 6

    How to load a class from a .jar Android

  7. 7

    FindClass on a Class that was loaded from a jar file at runtime

  8. 8

    How to add class files and folders to a jar file from a directory in Java

  9. 9

    Load pytorch model from different working directory

  10. 10

    Failing to load class definition from jar

  11. 11

    How to make .jar from .class

  12. 12

    Load a class from a jar having dependency on another jar

  13. 13

    Java 6 runtime not overriding class called from within .jar dependency

  14. 14

    Java 6 runtime not overriding class called from within .jar dependency

  15. 15

    Extract jar from aar for cordova plugin, Class not found error at runtime

  16. 16

    Load and instantiate class from jar in class search path

  17. 17

    How to load resources from other JAR file

  18. 18

    How to load Hibernate entities from external JAR

  19. 19

    LWJGL 3 stbi_load_from_memory not working when in jar

  20. 20

    How to force a dll to load from current directory

  21. 21

    How to load a random file from a directory

  22. 22

    How to load images from a directory on the computer in Python

  23. 23

    How to load javascript file from another directory

  24. 24

    How to load images from a directory on the computer in Python

  25. 25

    How to load all the images from one Directory

  26. 26

    Cannot load main class from JAR file in Spark Submit

  27. 27

    unable to load java class from jar file file

  28. 28

    Python - how do i load my custom class methods from a root directory?

  29. 29

    How to load a library from JAR file loaded from browser?

HotTag

Archive