NEED help with plugin reload cmd! (.jar)

Discussion in 'Plugin Development' started by Minermax7, May 10, 2014.

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

    Minermax7

    Hey guys,
    I have been stuck on this problem for ages now and I just decided to ask for some help XD
    So, what a want to do is make a /ninjareload command, so I don't have to constantly restart my server to make a small change, I just want to use that command so it reloads the .jar file, without the whole server. ,_,
    Here's my code for now: (But the 'Ninjas' at the start is underlined red saying it can't be resolved to a type).
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("ninjareload")) {
    2. Ninjas plugin = Bukkit.getServer().getPluginManager().getPlugin("Ninjas");
    3. plugin.Reload();
    4. player.sendMessage(ChatColor.GREEN + "Plugin reloaded!");
    5. }


    Any help is GREATLY appreciated! Thanks. :)
     
  2. Offline

    Wizehh

    You can't reload an individual .jar, and if 'Ninjas' cannot be resolved to a type then either the class doesn't exist, or you have to import it (hover over 'Ninjas' and click 'import', or use the shortcut CTRL-SHIFT-O).
     
  3. Offline

    Polaris29

    Reloading an individual jar is much more trouble than it's worth. Just restart the entire server, it shouldn't take that long.

    Anyways, you'll have to use a classloader and reconstruct the classloader and load everything again each time you want to reload the jar. But I haven't tried reloading a jar on a Bukki server and I think Bukkit has its own classloader for plugins so you'll have to figure something out.

    Here's an example:
    Code:java
    1.  
    2. import java.io.File;
    3.  
    4. public class Test {
    5. public static void foo() throws Exception {
    6. ResourceClassLoader cl = new ResourceClassLoader(); // Just a class loader I made
    7. cl.getClassPath().loadResource(new File("F:/testplugin.jar"));
    8. Class<?> clazz = cl.loadClass("com.matthew0x40.testplugin.TestPlugin");
    9. clazz.getMethod("run").invoke(null);
    10. }
    11.  
    12. public static void bar() throws Exception {
    13. ResourceClassLoader cl = new ResourceClassLoader();
    14. cl.getClassPath().loadResource(new File("F:/testplugin.jar"));
    15. Class<?> test2 = cl.loadClass("com.matthew0x40.testplugin.TestPlugin");
    16. test2.getMethod("run").invoke(null);
    17. }
    18.  
    19. public static void main(String[] args) throws Exception {
    20. foo();
    21. Thread.sleep(15000); // Waits for me to replace the jar in the folder
    22. bar();
    23. }
    24. }
    25.  

    Here's the code for the TestPlugin class:
    Code:java
    1.  
    2. public class TestPlugin {
    3. public static void run() {
    4. System.out.println("hello world");
    5. }
    6. }
    7.  


    The jar that I replace the existing jar with has the same class but it says "goodbye world" instead of "hello world". This code does work and the output is:
    hello world
    goodbye world
     
Thread Status:
Not open for further replies.

Share This Page