2

Im trying to write scripting system for a Minecraft Spigot plugin and im not quite sure on how to give my scripts the information from my main jar file.

With this current code it will compile the script fine but give me an error on line "3" of the Compass.java file (The class declaration). From what I see the program doesnt know what "com.deft.core.scripts.DeftScript" is and im not quite sure on how to tell it this information.

I got my compiling code from this thread if its relevant

Compiling the Script:

    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

    List<String> optionList = new ArrayList<String>();
    optionList.add("-classpath");
    optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");


    Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(script));
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
    if (task.call()) {
        Object obj = null;
        try {
            URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./plugins/Deft-Core/").toURI().toURL()});
            Class<?> loadedClass;
            loadedClass = classLoader.loadClass("scripts.Compass");
            obj = loadedClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        if (obj instanceof DeftScript) {
            DeftScript deftScript = (DeftScript)obj;
            deftScript.onEnable(this);
        }
    } else {
        for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
            System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri());
        }
    }

Compass.java

package scripts;

public class Compass extends com.deft.core.scripts.DeftScript {
    @Override
    public void onEnabled() {
    }
}

DeftScript.java

package com.deft.core.scripts;

public abstract class DeftScript {
    public abstract void onEnable();
}

With com.deft.core.scripts.DeftScript being the source im trying to extend from the main jarfile.

Im pretty sure my problem lies from somewhere within here:

List<String> optionList = new ArrayList<String>();
    optionList.add("-classpath");
    optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");

EDIT:

Fixed my problem with setting up the class path. My jar file in question was location in another location from what I specified. I switch out the optionList.add calls with one

optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar"));

All tho this fixed my initial question now it as started causing a new error. Apon trying to run my newly compiled class file it is giving me an error

java.lang.NoClassDefFoundError: com/deft/core/scripts/DeftScript
    at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_45]
    at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_45]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_45]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
    at com.deft.core.main.DeftCore.onEnable(DeftCore.java:79) ~[?:?]
Community
  • 1
  • 1
Kuliu
  • 171
  • 2
  • 12
  • Could you show us your DeftScript class as well as where you are calling the compile script method from? – Adrian Sohn Jul 07 '15 at 16:59
  • @AdrianSohn Ive actually fixed my initial problem with the linking. Now its actually compiling the script but when I go to run the script that was compiled it cant find the "com.deft.core.scripts.DeftScript" and it throws a NoClassDefFoundError. Which completely baffles me because it needed this class to compile! – Kuliu Jul 07 '15 at 17:09

0 Answers0