12.1. Include Neo4j in your project

12.1.1. Add Neo4j to the build path
12.1.2. Add Neo4j as a dependency
12.1.3. Starting and stopping

After selecting the appropriate edition for your platform, embed Neo4j in your Java application by including the Neo4j library jars in your build.

12.1.1. Add Neo4j to the build path

Get the Neo4j libraries from one of these sources:

Add the jar files to your project:

JDK tools
Append to -classpath
Eclipse
  • Right-click on the project and then go Build Path → Configure Build Path. In the dialog, choose Add External JARs, browse to the Neo4j lib/ directory and select all of the jar files.
  • Another option is to use User Libraries.
IntelliJ IDEA
See Libraries, Global Libraries, and the Configure Library dialog
NetBeans
  • Right-click on the Libraries node of the project, choose Add JAR/Folder, browse to the Neo4j lib/ directory and select all of the jar files.
  • You can also handle libraries from the proejct node, see Managing a Project’s Classpath.

12.1.2. Add Neo4j as a dependency

For an overview of the main Neo4j artifacts, see Table 1.2, “Neo4j editions”. The artifacts listed there are top-level artifacts that will transitively include the actual Neo4j implementation. You can either go with the top-level artifact or include the individual components directly. The examples included here use the top-level artifact approach.

Maven

Maven dependency. 

<project>
...
 <dependencies>
  <dependency>
   <groupId>org.neo4j</groupId>
   <artifactId>neo4j</artifactId>
   <version>${neo4j-version}</version>
  </dependency>
  ...
 </dependencies>
...
</project>

Where ${neo4j-version} is the intended version and the artifactId is found in Table 1.2, “Neo4j editions”.

Ivy

Make sure to resolve dependencies from Maven Central, for example using this configuration in your ivysettings.xml file:

<ivysettings>
  <settings defaultResolver="main"/>
  <resolvers>
    <chain name="main">
      <filesystem name="local">
        <artifact pattern="${ivy.settings.dir}/repository/[artifact]-[revision].[ext]" />
      </filesystem>
      <ibiblio name="maven_central" root="http://repo1.maven.org/maven2/" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>

With that in place you can add Neo4j to the mix by having something along these lines to your ivy.xml file:

..
<dependencies>
  ..
  <dependency org="org.neo4j" name="neo4j" rev="${neo4j-version}"/>
  ..
</dependencies>
..

Where ${neo4j-version} is the intended version and the name is found in Table 1.2, “Neo4j editions”.

12.1.3. Starting and stopping

To create a new database or ópen an existing one you instantiate an EmbeddedGraphDatabase.

GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
registerShutdownHook( graphDb );
[Note]Note

The EmbeddedGraphDatabase instance can be shared among multiple threads. Note however that you can’t create multiple instances poting to the same database.

To stop the database, call the shutdown() method:

graphDb.shutdown();

To make sure Neo4j is shut down properly you can add a shutdown hook:

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 example before it's completed)
    Runtime.getRuntime().addShutdownHook( new Thread()
    {
        @Override
        public void run()
        {
            graphDb.shutdown();
        }
    } );
}

If you want a read-only view of the database, use EmbeddedReadOnlyGraphDatabase.

To start Neo4j with configuration settings, a Neo4j properties file can be loaded like this:

Map<String, String> config = EmbeddedGraphDatabase.loadConfigurations( pathToConfig
                                                                       + "neo4j.properties" );
GraphDatabaseService graphDb = new EmbeddedGraphDatabase(
        "target/database/location", config );

Or you could of course create you own Map<String, String> programatically and use that instead.

For configuration settings, see Chapter 2, Configuration & Performance.