Embed neo4j-enterprise within your java application

The Neo4j Java Reference Documentation generally describes how to embed Neo4j Community Edition within your Java application. If you are licensed for Neo4j Enterprise, this article will guide you through setting up the project to use Neo4j Enterprise embedded within your application.

Create a new project

First you will need to download IntelliJ community edition. Once done, create a New Project>Java>Command Line App.

Specify its name embedded and the base package

Configure the maven repository

Create a folder .m2 in your home directory

Create the ${HOME}/.m2/settings.xml file as following.

Refer to the KB Dependency location

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
            <password>modify password there</password>

Create a project pom.xml file

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.

Create a new text file at the root of your project using intelliJ and name it pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	<!-- maven specific -->

	<!-- your own project settings-->
    <description>Embedded demo project</description>

	<!-- Where to download the artifact-->
            <name>Neo4j Enterprise Artifacts</name>

    <!-- Which artifact is required-->

			<!-- com.neo4j for enterprise, doc needs to be fixed: https://neo4j.com/docs/java-reference/3.5/tutorials-java-embedded/#editions -->

	<!-- to specify which version of java to compile the source code, here: java 11-->
                    <source>11</source> <!-- source code version -->
                    <target>11</target> <!-- binary destination version -->

Create the java class

In intelliJ, right-click on src and choose New > Java Class and name it EmbeddedNeo4j. You can paste the following code or follow the java reference manual

 * Licensed to Neo4j under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo4j licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.

import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.EnterpriseGraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;

public class EmbeddedNeo4j
    private static final File databaseDirectory = new File( "/Users/jphoulchand/neo4j/test/embedded-neo4j-enterprise/" );

    public String greeting;

    // tag::vars[]
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    // end::vars[]

    // tag::createReltype[]
    private enum RelTypes implements RelationshipType
    // end::createReltype[]

    public static void main( final String[] args ) throws IOException
        EmbeddedNeo4j hello = new EmbeddedNeo4j();

    void createDb() throws IOException
        FileUtils.deleteRecursively( databaseDirectory );

        // tag::startDb[]
        graphDb = new EnterpriseGraphDatabaseFactory().newEmbeddedDatabase( databaseDirectory );
        registerShutdownHook( graphDb );
        // end::startDb[]

        // tag::transaction[]
        try ( Transaction tx = graphDb.beginTx() )
            // Database operations go here
            // end::transaction[]
            // tag::addData[]
            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello, " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "World!" );

            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );
            // end::addData[]

            // tag::readData[]
            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );
            // end::readData[]

            greeting = ( (String) firstNode.getProperty( "message" ) )
                    + ( (String) relationship.getProperty( "message" ) )
                    + ( (String) secondNode.getProperty( "message" ) );

            // tag::transaction[]
        // end::transaction[]

    void removeData()
        try ( Transaction tx = graphDb.beginTx() )
            // tag::removingData[]
            // let's remove the data
            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
            // end::removingData[]


    void shutDown()
        System.out.println( "Shutting down database ..." );
        // tag::shutdownServer[]
        // end::shutdownServer[]

    // tag::shutdownHook[]
    private static void registerShutdownHook( final GraphDatabaseService graphDb )
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
            public void run()
        } );
    // end::shutdownHook[]

  • Last Modified: 2020-09-15 13:07:09 UTC by Jérémie Phoulchand.
  • Relevant for Neo4j Versions: 3.5.
  • Relevant keywords intellij, enterprise, embedded,installation.