-1

Im currently getting an error on thie line loadedClass = classLoader.loadClass("scripts.Compass");

Somehow the file compiles fine but when I try to load the class it wont work because it says its missing the class com/deft/core/scripts/DeftScript

Compiling & Instantiating:

    File script = new File("./plugins/Deft-Core/scripts/Compass.java");
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

    List<String> optionList = new ArrayList<String>();
    optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/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 | InstantiationException | IllegalAccessException | MalformedURLException e) {
            e.printStackTrace();
        } 

        if (obj instanceof DeftScript) {
            DeftScript deftScript = (DeftScript)obj;
            deftScript.onEnable();
        }
    } 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;
import com.deft.core.scripts.DeftScript;
public class Compass extends DeftScript {
    @Override
    public void onEnable() {
    }
}

DeftScript.java

package com.deft.core.scripts;

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

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) ~[?:?]
Kuliu
  • 171
  • 2
  • 12
  • I'm afraid you're going to need to give us some information about how you are including your dependencies if you want help here. – Tim Biegeleisen Jul 08 '15 at 01:55
  • @TimBiegeleisen Not quite sure on what your asking. – Kuliu Jul 08 '15 at 01:57
  • Are you using Maven, Gradle, Ant, etc. to include your JAR dependencies when you build? We need to see how the class in question might not be getting loaded. – Tim Biegeleisen Jul 08 '15 at 01:58
  • @TimBiegeleisen Im using Eclipse for the "Compiling & Instantiating". The jar file gets used as a plugin file for **Minecraft Spigot / Bukkit** servers and the Compass.java is out of the project. hints why im trying to compile and initialize it. – Kuliu Jul 08 '15 at 02:01
  • 1
    possible duplicate of [ClassCastException on custom class loading](http://stackoverflow.com/questions/31282985/classcastexception-on-custom-class-loading) – Unihedron Jul 09 '15 at 05:35

1 Answers1

0

You need to load com.deft.core.scripts.DeftScript first before you load scripts.Compass as it is extending it. Or you can make sure you drop the parent class/jar in parent class loader path.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Not quite sure how i would load this before hand. See the "Compiling & Instantiating" and the DeftScipt class is stored in its own jar file. hints optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar")); Im compiling the Compass.java file out side of this jar file. The directory is shown at the top. – Kuliu Jul 08 '15 at 02:06
  • @Kuliu Check this : http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – Juned Ahsan Jul 08 '15 at 02:07
  • This seems to work and allow it to complete the instance but When i try and cast it, it throws a 'java.lang.ClassCastException: scripts.Compass cannot be cast to com.deft.core.scripts.DeftScript' Event though it extends the Class deftscript. – Kuliu Jul 08 '15 at 02:21
  • @Kuliu your original question is answered...you can post a new question for further help from a broader forum. – Juned Ahsan Jul 08 '15 at 02:24