mardi 2 avril 2019

I can't load class from .class file

I was following anwser from Method to dynamically load java class files

path lead as to: F:\Java\ProgramowanieWJezykuJavaPWR\lab02\out\production\lab02\classes Where I put KnapsackBForceAlgorithm.class (which is from package It says that i can't find class KnapsackBForceAlgorithm java.lang.ClassNotFoundException:

KnapsackProblem.KnapsackSolvingAlgorithms i wrote in other project a few days ago)

So classBinName is KnapsackProblem.KnapsackSolvingAlgorithms.KnapsackBForceAlgorithm

It says that i can't find class KnapsackBForceAlgorithm java.lang.ClassNotFoundException:

public class AlgorithClassLoader{
public Method solve;
private Object solverObject;

public AlgorithClassLoader(String classBinName, InstanceProblem problem)
{
    try{
        String path = new File(AlgorithClassLoader.class.getProtectionDomain().getCodeSource().getLocation()
                .toURI()).getPath();
        path=path+"\\classes";
        File classFile=new File(path);
        URL[] urls = new URL[]{classFile.toURI().toURL()};
        ClassLoader classLoader=new URLClassLoader(urls);
        Class<?> loadedMyClass = classLoader.loadClass(classBinName);
        Constructor[] allConstructors = loadedMyClass.getDeclaredConstructors();
        solverObject = allConstructors[0].newInstance(problem);
        solve=loadedMyClass.getMethod("Solve");
    }
    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public KnapsackSolvingResult Solve() throws InvocationTargetException, IllegalAccessException {
    return (KnapsackSolvingResult) solve.invoke(solverObject);
}


}





Aucun commentaire:

Enregistrer un commentaire