Prev |  | Next

## 33.1. Include Neo4j in your project

After selecting the appropriate edition for your platform, embed Neo4j in your Java application by including the Neo4j library jars in your build. The following sections will show how to do this by either altering the build path directly or by using dependency management.

### 33.1.1. Add Neo4j to the build path

Get the Neo4j libraries from one of these sources:

- Extract a Neo4j download zip/tarball, and use the jar files found in the lib/ directory.
- Use the jar files available from Maven Central Repository

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 project node, see Managing a Project’s Classpath.

### 33.1.2. Editions

The following table outlines the available editions and their names for use with dependency management tools.

| Tip
Follow the links in the table for details on dependency configuration with Apache Maven, Apache Buildr, Apache Ivy, Groovy Grape, Grails, Scala SBT!

Neo4j editions

Edition | Dependency | Description | License
--- | --- | --- | ---
Community | org.neo4j:neo4j | a high performance, fully ACID transactional graph database | GPLv3
Enterprise | org.neo4j:neo4j-enterprise | adding advanced monitoring, online backup and High Availability clustering | AGPLv3

| Note
The listed dependencies do not contain the implementation, but pulls it in transitively.

For more information regarding licensing, see the Licensing Guide.

### 33.1.3. Add Neo4j as a dependency

You can either go with the top-level artifact from the table above 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>2.1.0-M01</version>
      </dependency>
      ...
     </dependencies>
    ...
    </project>

Where the `artifactId` is found in the editions table.

#### Eclipse and Maven

For development in Eclipse, it is recommended to install the m2e plugin and let Maven manage the project build classpath instead, see above. This also adds the possibility to build your project both via the command line with Maven and have a working Eclipse setup for development.

#### 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="2.1.0-M01"/>
      ..
    </dependencies>
    ..

Where the `name` is found in the editions table above

#### Gradle

The example below shows an example gradle build script for including the Neo4j libraries.

    def neo4jVersion = "2.1.0-M01"
    apply plugin: 'java'
    repositories {
       mavenCentral()
    }
    dependencies {
       compile "org.neo4j:neo4j:${neo4jVersion}"
    }

Where the coordinates (`org.neo4j:neo4j` in the example) are found in the editions table above.

### 33.1.4. Starting and stopping

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

    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    registerShutdownHook( graphDb );

| Note
The `EmbeddedGraphDatabase` instance can be shared among multiple threads. Note however that you can’t create multiple instances pointing 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 application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }

#### Starting an embedded database with configuration settings

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

    GraphDatabaseService graphDb = new GraphDatabaseFactory()
        .newEmbeddedDatabaseBuilder( storeDir )
        .loadPropertiesFromFile( pathToConfig + "neo4j.properties" )
        .newGraphDatabase();

Configuration settings can also be applied programmatically, like so:

    GraphDatabaseService graphDb = new GraphDatabaseFactory()
        .newEmbeddedDatabaseBuilder( storeDir )
        .setConfig( GraphDatabaseSettings.nodestore_mapped_memory_size, "10M" )
        .setConfig( GraphDatabaseSettings.string_block_size, "60" )
        .setConfig( GraphDatabaseSettings.array_block_size, "300" )
        .newGraphDatabase();

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

#### Starting an embedded read-only instance

If you want a read-only view of the database, create an instance this way:

    graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(
            "target/read-only-db/location" )
            .setConfig( GraphDatabaseSettings.read_only, "true" )
            .newGraphDatabase();

Obviously the database has to already exist in this case.

| Note
Concurrent access to the same database files by multiple (read-only or write) instances is not supported.

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |