Hi, I'm new to neo4j and trying to build and run the HelloWorldExample.java as described at https://neo4j.com/docs/java-reference/current/java-embedded/setup/ . It builds, but then gets a class not found error (stack trace below), for a class that doesn't appear to be in the neo4j-5.3.0.jar file under /org/neo4j/dbms . Is the example out of date, or have I made a mistake. I'd be grateful for advice.
all the best,
Adam
$ java -cp target/my-app-1.jar:lib/* com.articulate.HelloWorldExample
Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/dbms/DatabaseStateService
at org.neo4j.dbms.api.DatabaseManagementServiceBuilderImplementation.getEditionFactory(DatabaseManagementServiceBuilderImplementation.java:91)
at org.neo4j.dbms.api.DatabaseManagementServiceBuilderImplementation.newDatabaseManagementService(DatabaseManagementServiceBuilderImplementation.java:82)
at org.neo4j.dbms.api.DatabaseManagementServiceBuilderImplementation.build(DatabaseManagementServiceBuilderImplementation.java:78)
at org.neo4j.dbms.api.DatabaseManagementServiceBuilder.build(DatabaseManagementServiceBuilder.java:42)
at com.articulate.HelloWorldExample.main(HelloWorldExample.java:45)
Caused by: java.lang.ClassNotFoundException: org.neo4j.dbms.DatabaseStateService
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 5 more
Hi, Adam! Thanks for reaching out. I think our documentation could be more clear here. Neo4j consists of more than one jar, so there would be quite a few to list in your classpath for everything to operate properly. We should recommend using a package manager (such as Maven or Gradle) as the first priority and avoid specifying all the dependencies manually.
I will send this feedback on to the documentation team. Could you try adding the Neo4j dependency through Maven or Gradle and let me know if that works for you? Let me know if you have any other questions!
Hi Jennifer, yes I've used Maven and it indeed includes several hundred jars. The problem appear to be that a class called org/neo4j/dbms/DatabaseStateService is not in neo4j-5.3.0.jar file under /org/neo4j/dbms even though neo4j-5.3.0.jar contains several other classes in that path. I don't know why Maven doesn't pick up that dependency and so that was my question. I'd be grateful for further advice.
It looks like it's picking it up on a test app we built here, although the neo4j-dbms-5.3.0 is a transitive dependency.
My colleague just created a simple app, added this to the pom:
org.neo4j
neo4j
5.3.0
and wrote this into the main class:
public class Main {
public static void main(String args) {
var managementService = new DatabaseManagementServiceBuilder(Path.of("/Users/gerritmeier/temp")).build();
var graphDb = managementService.database( GraphDatabaseSettings.DEFAULT_DATABASE_NAME );
registerShutdownHook(managementService);
}
private static void registerShutdownHook( final DatabaseManagementService managementService ) {
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
managementService.shutdown();
}
} );
}