Search code examples
javamavenneo4jluceneclassloader

.jar conflict causes java.lang.RuntimeException: Error starting org.neo4j.kernel.EmbeddedGraphDatabase exception


Because of different class versions in different .jar files I got this exception:

java.lang.RuntimeException: Error starting org.neo4j.kernel.EmbeddedGraphDatabase, E:\neo4j at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:333) at org.neo4j.kernel.EmbeddedGraphDatabase.(EmbeddedGraphDatabase.java:63) at org.neo4j.graphdb.factory.GraphDatabaseFactory$1.newDatabase(GraphDatabaseFactory.java:92) at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:198) at org.neo4j.graphdb.factory.GraphDatabaseFactory.newEmbeddedDatabase(GraphDatabaseFactory.java:69) at neo4j_lucene.conflict_solver.ConfilctSolver.createDb(ConfilctSolver.java:55) at neo4j_lucene.conflict_solver.ConfilctSolver.main(ConfilctSolver.java:35)

despite I'm using ClassLoader for solving this problem, but again I get same exception. Here is my code:

try {
        CustomClassLoader ccl = new CustomClassLoader();
        Object object;
        Class clas;
        clas = ccl
                .loadClass("org.neo4j.graphdb.factory.GraphDatabaseFactory");

        object = clas.newInstance();

        graphDb = ((GraphDatabaseFactory) object)
                .newEmbeddedDatabase(DB_PATH);      

    } catch (Exception e) {
        e.printStackTrace();
    }

Custom class loader code:

public class CustomClassLoader extends ClassLoader {
private String jarFile = "C:/Users/RaufA/Desktop/test.jar"; // Path
                                                                            // to
                                                                            // the
                                                                            // jar
                                                                            // file
private Hashtable classes = new Hashtable(); // used to cache already
                                                // defined classes

public CustomClassLoader() {
    super(CustomClassLoader.class.getClassLoader()); // calls the parent
                                                        // class
                                                        // loader's
                                                        // constructor
}

public Class loadClass(String className) throws ClassNotFoundException {
    return findClass(className);
}

public Class findClass(String className) {
    byte classByte[];
    Class result = null;

    result = (Class) classes.get(className); // checks in cached classes
    if (result != null) {
        return result;
    }

    try {
        return findSystemClass(className);
    } catch (Exception e) {
    }

    try {
        JarFile jar = new JarFile(jarFile);         
        JarEntry entry = jar.getJarEntry(className + ".class");     
        System.out.println(className+".class");
        InputStream is = jar.getInputStream(entry);
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        int nextValue = is.read();
        while (-1 != nextValue) {
            byteStream.write(nextValue);
            nextValue = is.read();
        }

        classByte = byteStream.toByteArray();
        result = defineClass(className, classByte, 0, classByte.length,
                null);
        classes.put(className, result);
        System.out.println(">>>>result: " + result);
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

}

What else should I do?


Solution

    1. Add this project to eclipse -> https://github.com/lagodiuk/neo4j-uber-jar.
    2. Use mvn-install and create ~SNAPSHOT.jar
    3. Add that .jar to your project (which has conflict)
    4. Remove neo4j maven dependency from that project.