External JAR loading, a hacky situation.

Discussion in 'Plugin Development' started by AstramG, May 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    AstramG

    Hello, today I'm looking for a way to load JAR files from an external source. By this, I'm referring to loading class files that aren't in the plugins directory. My situation is that I need to call a method in a class that extends Module. However, this method is in a different class, that's in a different jar, that's in a different directory. Therefore it may be a bit tricky and I just can't seem to figure it out.
     
  2. Offline

    RawCode

    post your coding attempts to fix issue.
    did you visited stackoverflow forums (they have answer)?
    did you checked plugins that implement externalclassloading on bukkitdev?
     
  3. Offline

    mrkirby153

    AstramG

    So why exactly do you need to do this?
     
  4. Offline

    AstramG

    RawCode
    The issue is that I have no code. I have just a little bit of testing, I honestly don't know where to start. I've tried doing what they have over at the stackoverflow forums but still couldn't find a working method for this task. I haven't check for plugins that do that on BukkitDev because I honestly don't think they would. But I can check.

    mrkirby153
    I'm creating a certain server infrastructure where this is required.
     
  5. Offline

    mrkirby153

    AstramG

    Can you elaborate a bit more on that please? Like what you are planning to do and why? (Or is everything top secret XD)
     
  6. Offline

    RawCode

    no code no help.

    post whatever you have.
     
  7. Offline

    AstramG

    mrkirby153
    I'd like to let it remain untold, I explained the situation pretty well in my first post. Shouldn't that be all I need or are you just too nosy and need to know the exact specifications. I was pretty clear in stating that I need to load classes from an external jar in an external path.

    RawCode
    The mere fact that I have no idea where to start is limiting my progress, which is why I'm seeking help in the first place. However, I can give you one thing though.

    JarFile jarFile = new JarFile(file);
     
  8. Offline

    TGRHavoc

    AstramG
    We cannont help unless you provide more information... For all we know, you're doing this for malicious purposes (Detering us from helping you)...
     
  9. Offline

    AstramG

    TGRHavoc
    I'm definetly not uploading this plugin to Bukkit, it's for a private plugin so don't worry. I suppose I'll just lay it out to you guys. I'm trying to create a plugin that loads external JARs. It will be a Minigame API and it will load the minigames from another path. I've been with Bukkit for more than two years, I wouldn't ever do anything malicious.
     
  10. Offline

    RawCode

    JarFile jarFile = new JarFile(file); AstramG

    this line not related to subject ever remotely.
    if you not going to show your super secret code - seek help elsewhere
     
  11. Offline

    TGRHavoc

  12. Offline

    AstramG

    RawCode
    You're obviously very confused and don't like to read. I've stated above numerous times that I have literately no code, why can't you understand that? It's not a secret, it's that I have none. It's like I'm talking to a wall. Also, that line is obviously related to the subject because you can loop through it and get all the classes. Obviously you're inexperienced with this situation, and I'd prefer you not to post your negative posts when you don't even decide to read or understand the situation at all.

    TGRHavoc
    I've already tried this method out. However, I couldn't get it too check if the class implements Module. Module is the interface I use for the main class of the external jar files.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  13. Offline

    Not2EXceL

    boolean Class.isAssignableFrom(Class<T>)

    And you supposedly looked and have no idea where to look. Seriously Google could have told you how to check if a class implements an interface. You really only know the basics of jaba
     
  14. Offline

    AstramG

    Not2EXceL I apologize, plus I have googled this issue. However I didn't google about checking if a class implements another, thanks for that. I'd just like you to know though that I have gotten much further than the basics of Java. I've been creating large minigames for YouTubers with over 5 million subscribers. It's just that I don't know this specific topic, thank you for assisting me though.
     
  15. Offline

    Garris0n

    The thing is, somebody who is creating a large minigame for a YouTuber with 5 million subscribers should've known to search for something like "java check if class implements interface". In fact, to be honest, you probably should've assumed it was in the reflection API and gone straight to the JavaDocs to find it. Either way, you shouldn't have had to ask others for the method, it was not that hard to find.
     
  16. Offline

    oaschi

  17. Offline

    Garris0n

    AstramG likes this.
  18. Offline

    AstramG

    Thank you! :)
    Sorry for asking for that simple method, honestly I've just been on my phone all day. Couldn't reach my computer since last night.
     
  19. Offline

    oaschi

  20. Offline

    Not2EXceL

    It doesn't even touch Reflection. It's basic Class loading and Enumeration.
     
    jthort likes this.
  21. Offline

    AstramG

    I've taken into account everything you've guys said but it's still giving me similar issues to what I've had in the first place before I created this thread. It's saying now that it can't find a class when it's clearly there. Here is the code for loading the modules.
    Code:java
    1. private void loadModules() throws Exception {
    2. System.out.println("Loading Modules...");
    3. File moduleFolder = new File("Modules/");
    4. if (!moduleFolder.isDirectory())
    5. throw new Exception("Module's folder is non-existant in the root directory!");
    6. for (File file : moduleFolder.listFiles()) {
    7. if (file.toString().endsWith(".jar")) {
    8. JarFile jarFile = new JarFile(file);
    9. Enumeration<JarEntry> e = jarFile.entries();
    10.  
    11. URL[] urls = { new URL("jar:file:" + file.getPath() +"!/") };
    12. URLClassLoader cl = URLClassLoader.newInstance(urls);
    13.  
    14. while (e.hasMoreElements()) {
    15. JarEntry je = (JarEntry) e.nextElement();
    16. if(je.isDirectory() || !je.getName().endsWith(".class")){
    17. continue;
    18. }
    19.  
    20. String className = je.getName().substring(0,je.getName().length()-6);
    21. className = className.replace('/', '.');
    22. Class<?> c = cl.loadClass(className);
    23. if (c.isAssignableFrom(MinetrixModule.class)) {
    24. Method enable = c.getMethod("run");
    25. enable.invoke(c);
    26. }
    27. }
    28. }
    29. }
    30. }

    This is line 48 for the error below:
    Code:java
    1. Class<?> c = cl.loadClass(className);

    However it's giving me this error:
    http://hastebin.com/cuxewogava.avrasm
     
  22. Offline

    mazentheamazin

    AstramG
    Honestly, I think you should have a .yml file which would have a main value which would link to the module. And you would use class.forName(path).getMethod("run").invoke(c);
     
  23. Offline

    AstramG

    Hmm, you're probably right but I am trying to make it as dynamic as possible. Anybody else have any suggestions?
     
  24. Offline

    Not2EXceL

    AstramG
    Code:
    enable.invoke(c);
    
    based on just that, its clear you don't understand the Reflection api.

    Also due to how the jvm classloader works, you're creating your URLClassLoader instance wrong. The craftbukkit classloader won't be able to find the classes.
     
  25. Offline

    AstramG

    Not2EXceL I had a feeling something like that would occur. How do I register it with the CraftBukkit classloader?
     
  26. Offline

    fireblast709

  27. Offline

    Iroh

    Removed offtopic conversation.
     
  28. Offline

    AstramG

    fireblast709 That's what I currently am using, however Not2EXceL said that I would need to use a separate class loader for Bukkit.

    Iroh Thanks!
     
  29. Offline

    Not2EXceL

    No the way you are creating your URLClassLoader is why it's not able to locate the classes -_-
     
  30. Offline

    Garris0n

    Point taken, Class is just used in tandem with reflection quite a bit.
     
    Not2EXceL likes this.
Thread Status:
Not open for further replies.

Share This Page