cancel
Showing results for 
Search instead for 
Did you mean: 

java.lang.ClassNotFoundException when running embedded Neo4j example

jana_cyrus
Node

Hi,
I only used Neo4j through Cypher querys in the past, now I need to write code for a Neo4j graphdatabase for my bachelor thesis. I set up a Maven project in Eclipse following these instructions: How to set up a maven project with Neo4j in Eclipse and Including Neo4j in your project

To get familiar with using embedded Neo4j I tried to run this example: Hello World- Java Reference
I also found the code on Github and copied it, so this cannot be a problem in the code.

Code on GitHub

In the beginning I declare an enum for the different relationship types which I then use in the method createDb(). In the main method I create a new Object from the class and run the method createDb().

private enum RelTypes implements RelationshipType{
        KNOWS
}

void createDb() throws IOException{
            ...//database created
            
            try (Transaction tx = graphDb.beginTx()){    
                firstNode = tx.createNode();
                secondNode = tx.createNode();
                relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS); 
                tx.commit();
            }

public static void main(String [] args) throws IOException{
         Setup hello = new Setup();
         hello.createDb();
         ...

That's when I get the java.lang.ClassNotFoundExecption.

Exception in thread "main" java.lang.NoClassDefFoundError: bachelorarbeit/masymos_44/Setup$RelTypes
	at bachelorarbeit.masymos_44.Setup.createDb(Setup.java:58)
	at bachelorarbeit.masymos_44.Setup.main(Setup.java:122)
Caused by: java.lang.ClassNotFoundException: bachelorarbeit.masymos_44.Setup$RelTypes
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 2 more

I use Neo4j version 4.4.4 and the JDK 11.0.14 with a SE 1.7.
I already tried to clean, refresh and update the project and checked the classpaths. I also deleted and reinstalled Eclipse and created the project again but I still get the same error. The same error also occurs when I try to use other classes after I created the database. Eclipse doesn't show any errors or warnings in the project.

Does anyone knows how to fix this error? Do I do something wrong when setting up the project or do I maybe miss any maven dependencies or .jar files?

I use Maven 4.0.0. My pom.xml looks like this:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.neo4j</groupId>
    	<artifactId>neo4j</artifactId>
    	<version>4.4.4</version>
    </dependency>
    <dependency>
    	<groupId>org.neo4j</groupId>
    	<artifactId>neosemantics</artifactId>
    	<version>4.4.0.1</version>
    </dependency>
  </dependencies>

Any help is appreciated. Thanks a lot,
Jana

3 REPLIES 3

sameer_gijare14
Graph Buddy

In simple java this error means that the new enum class should be in class path.
May be this will give you an insight into what you are trying to accomplish.
Many thanks
Sameer

Anyway welcome to Neo4j community
-Mr. Sameer Gijare

jana_cyrus
Node

Hi,
thank you for your answer. Funny enough, I just added the first line in the main method in my code and suddenly it worked.

public static void main(String [] args) throws IOException{
    	RelTypes rt = RelTypes.KNOWS;
    	Setup hello = new Setup();
    	hello.createDb();

Do you know why?
I will do a larger project with neosemantics where I will need to declare new objects from different classes inside the database but I always got the same error as above.

Thanks, Jana