Disappearing jar entry on boot using SPI

So, in my application, I have a plugin system that I wrote using SPI (Service Provider Interface), so I have a jar file called scripts.jar that I store somewhere (note: this is not the path to classes), then I load the script using this method:

/**
 * Loads a script from the given name, regardless of case.
 * @param name script name
 * @return the loaded Script
 */
public static Script loadScript(Client c, String name) {
    Script script = null;
    try {
        URLClassLoader loader = new URLClassLoader(new URL[]{new File(Config.CONF_DIR, "scripts.jar").toURI().toURL()});
        ServiceLoader serviceLoader = ServiceLoader.load(Script.class, loader);
        serviceLoader.reload();
        Iterator<Script> scripts = serviceLoader.iterator();
        while(scripts.hasNext()) {
            Script cur = scripts.next();
            if(cur.getClass().getSimpleName().equalsIgnoreCase(name)) {
                script = cur;
                break;
            }
        }
    } catch(Exception e) {
        log.warn("Error loading script "+name, e);
    }
    return script;
}

Now it works fantastically when I run my program. I can easily load any script into the jar.

The problem occurs when I reload the script - that is, I change the file and overwrite scripts.jar, and then try to load any script. I get this error:

    Exception in thread "anjin_san:test" java.util.ServiceConfigurationError: org.stork.script.Script: Error reading configuration file
        at java.util.ServiceLoader.fail(ServiceLoader.java:207)
        at java.util.ServiceLoader.parse(ServiceLoader.java:284)
        at java.util.ServiceLoader.access$200(ServiceLoader.java:164)
        at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:332)
        at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:415)
        at org.stork.script.Script.loadScript(Script.java:408)
        at org.stork.Client$2.run(Client.java:95)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.io.FileNotFoundException: JAR entry META-INF/services/org.stork.script.Script not found in C:\Users\stork\Documents\bot-sama\etc\scripts.jar
        at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
        at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:132)
        at java.net.URL.openStream(URL.java:1010)
        at java.util.ServiceLoader.parse(ServiceLoader.java:279)
        ... 6 more

So, he says that the jar entry cannot be found, but I can check it there quite easily:

    C:\Users\stork\Documents\bot-sama\etc>jar tvf scripts.jar
     0 Thu Sep 03 16:58:00 BST 2009 META-INF/
   102 Thu Sep 03 16:57:58 BST 2009 META-INF/MANIFEST.MF
     0 Thu Sep 03 16:35:20 BST 2009 META-INF/services/
   482 Thu Sep 03 16:57:52 BST 2009 META-INF/services/org.stork.script.Script
   ...

. , script, , .

, , ?

+3
2

. JAR , . , .

, Windows , XP Home cacls .


: ServiceLoader, reload. ServiceLoader , , , ServiceLoader , . , , reload .

+4

, URL- , openConnection(). :

private void disableURLConnectionCache() {

    new URLConnection(null) {
        @Override
        public void connect() throws IOException {}
    }.setDefaultUseCaches(false);
}
0

Source: https://habr.com/ru/post/1716810/


All Articles