1
votes

I learn REST API with Java and tried run this simple code, but I got error. Something wrong with this part of code: RestAPI graphDb = new RestAPI.... I use this external JAR (http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar)

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.RestGraphDatabase;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;

public class CypherQuery {

    public static void main(String[] args) {

        RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data/");

        QueryEngine engine=new RestCypherQueryEngine(graphDb);  
        QueryResult<Map<String,Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);  

        Iterator<Map<String, Object>> iterator=result.iterator();  
        if(iterator.hasNext()) {  
          Map<String,Object> row= iterator.next();  
          System.out.println("Total nodes: " + row.get("total"));

        }
    }
}

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/Response$StatusType
    at org.neo4j.rest.graphdb.RestAPIFacade.<init>(RestAPIFacade.java:295)
    at cz.mendelu.bp.CypherQuery.main(CypherQuery.java:19)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.core.Response$StatusType
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more
1
How are you building and executing this program? ClassNotFoundException is a pretty standard Java issue.chrylis -cautiouslyoptimistic-
Spring->CypherQuery class Run As Java ApplicationEdWood
That's a Maven repository. You shouldn't download and import random jars; they don't include dependencies such as JAX-WS. Use proper dependency management, such as through m2eclipse.chrylis -cautiouslyoptimistic-
Thx, but I am not sure how it works, you mean Eclipse market place? I tried find dependency at mvnrepository.com, but I didnt find it.EdWood
You need to learn to use a dependency-management tool, and Maven is the default for most of the standard tools (though Gradle is mostly interoperable). m2eclipse is an Eclipse plugin (available in the marketplace, I think) that lets Eclipse use Maven dependency management. In a Maven project, adding the Neo4J dependency will also include all the jars it needs, such as JAX-WS.chrylis -cautiouslyoptimistic-

1 Answers

1
votes

Like @chrylis pointed out, your error seems to be the issue that you don’t have the required jars and hence the errors. Now from your comments, I see that you are having difficulties with understanding maven and dependencies. So here's a simple guide that I made for you.

[Understand that this is NOT a one stop guide and this program might not run out of the box. Its running for me at the moment but it depends on many things including what version of neo4j you are running and several other configuration factors. Nonetheless, this should be sufficient to get you started. ]

You need to have maven installed on your system. There are few cool tutorials on maven. One is here. https://www.youtube.com/watch?v=al7bRZzz4oU&list=PL92E89440B7BFD0F6

But like me if you want a faster way, the new Eclipse Luna comes with maven installed for it. So download the new eclipse luna if you wish. Even with older eclipse versions you can go to marketplace and install the maven for eclipse.

Once done, make a maven quickstart project and replace your pom.xml file with the one below.

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>rash.experiments</groupId>
    <artifactId>neo4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>neo4j</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>neo4j-release-repository</id>
            <name>Neo4j Maven 2 release repository</name>
            <url>http://m2.neo4j.org/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-rest-graphdb</artifactId>
            <version>2.0.1</version>
        </dependency>
    </dependencies>
</project>

I assume that you have neo4j setup. I will not go into major details, but in the neo4j directory, under conf, in neo4j-server.properties file, you need to uncomment the line

org.neo4j.server.webserver.address = 0.0.0.0

This basically lets you access this server from your java code that you will run from another machine. After making this change, make sure that you restart your server and that its accessible to other machines. To test you can run http://ip.address.of.this.machine:7474 and the web portal that comes with neo4j should open up.

Note: I assume that you have some data in your database. This is required otherwise the program will fail. If you need some sample data, go to http://ip_address_of_your_neo4j_web_server:7474/ and load the movie graph database that comes with the installation.

Now lets code. Make this class in the project that you created above.

package rash.experiments.neo4j;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;

public class Neo4JRestTest
{
    public static void main(String args[])
    {
        RestAPI graphDb = new RestAPIFacade("http://192.168.1.8:7474/db/data/");
        QueryEngine engine = new RestCypherQueryEngine(graphDb);
        QueryResult<Map<String, Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
        Iterator<Map<String, Object>> iterator = result.iterator();
        if (iterator.hasNext())
        {
            Map<String, Object> row = iterator.next();
            System.out.print("Total nodes: " + row.get("total"));
        } 
    }
}

Now to run, you need to build your project first because maven will not download any of your jars that your specified in pom.xml until you have run it. So if you installed maven, go to the directory where you have your pom.xml and then write in the command line mvn clean package. This command will run and install all the dependencies and then will run your program. Since there are no test cases to run, it will succeed. But our goal was not to run any test cases. It was to download all the jars. Now that you have everything, you can run the java code and you will see your end results.

In case you are using maven from eclispe, then you right click your project and then do run as -> maven build. For the first time, a dialog will appear. Just write package in it and press enter. It will do the same things as above and bring all the jars. Then execute the program like you would above.

In case you get errors such as "No endpoint" or "error reading JSON", then understand that somehow the REST API is not able to read your graph.

check the property inside neo4j-server.properties. It should be whatever you are mentioning in your URL.

org.neo4j.server.webadmin.data.uri = /db/data/