import org.neo4j cannot be resolved?

Tags: ,



I am very new to Neo4j and I’d like to get started with an embedded Neo4j in a Java Application. I try to run a HelloWorld Application as follows.

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;

import static org.neo4j.driver.Values.parameters;

public class HelloWorldExample implements AutoCloseable
{
    private final Driver driver;
public HelloWorldExample( String uri, String user, String password )
{
    driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
}

@Override
public void close() throws Exception
{
    driver.close();
}

public void printGreeting( final String message )
{
    try ( Session session = driver.session() )
    {
        String greeting = session.writeTransaction( new TransactionWork<String>()
        {
            @Override
            public String execute( Transaction tx )
            {
                Result result = tx.run( "CREATE (a:Greeting) " +
                                                 "SET a.message = $message " +
                                                 "RETURN a.message + ', from node ' + id(a)",
                        parameters( "message", message ) );
                return result.single().get( 0 ).asString();
            }
        } );
        System.out.println( greeting );
    }
}

public static void main( String... args ) throws Exception
{
    try ( HelloWorldExample greeter = new HelloWorldExample( "bolt://localhost:7687", "neo4j", "password" ) )
    {
        greeter.printGreeting( "hello, world" );
    }
}

}

The Pom code is as follows.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.0.1</version>
  <name>neo4jtest</name>
   <build>
    <plugins>
        <plugin>
            <groupId>1</groupId>
            <artifactId>2</artifactId>
            <version>3.2</version>
            <configuration>
            <source>1.6</source>
            <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
   </build>
<dependencyManagement>
    <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-core</artifactId>
      <version>3.1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-bolt-driver</artifactId>
      <version>3.1.2</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
  </dependencyManagement>
</project>

Unfortunately I cannot run the code it raises “Exception in thread “main” java.lang.Error: Unresolved compilation problem: at HelloWorldExample.main(HelloWorldExample.java:46)”. Additionally, when hovering over the import lines I see “import org.neo4j cannot be resolved”.

Can somebody provide information about this?

Answer

As documented, in order to use neo4j’s Java driver, you need to specify the appropriate dependency:

<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>x.y.z</version>
</dependency>

The latest release version is 4.0.1.

[UPDATE]

Also, your pom.xml file has a lot of other issues. Try something like this instead:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>helloworld</artifactId>
    <version>1</version>
    <name>neo4jtest</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

</project>

This example POM file uses some dummy groupId, artifactId, and version values for itself (near the top). You should replace them with your own values (not values belonging to neo4j).



Source: stackoverflow