Neo4示例不起作用 - 找不到类

时间:2012-04-08 11:03:03

标签: maven-3 neo4j

我已经从教程部分复制并粘贴了一个简单的neo4j示例,但目前我收到了消息:

[INFO] Surefire report directory: /home/kama/ws-git/neo4jexample/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@17bd6a1
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.995 sec <<< FAILURE!

Results :

Failed tests:   firstTest(com.soebes.tutorials.Neo4JAppTest): com/soebes/tutorials/neo4jexample/RelationTypes

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

详细的错误消息:

java.lang.NoClassDefFoundError: com/soebes/tutorials/neo4jexample/RelationTypes
    at com.soebes.tutorials.neo4jexample.Neo4JApp.createDb(Neo4JApp.java:44)
    at com.soebes.tutorials.neo4jexample.Neo4JApp.main(Neo4JApp.java:25)
    at com.soebes.tutorials.Neo4JAppTest.firstTest(Neo4JAppTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:758)
    at org.testng.TestRunner.run(TestRunner.java:613)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
    at org.testng.TestNG.run(TestNG.java:974)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:76)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:161)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:101)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:115)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
Caused by: java.lang.ClassNotFoundException: com.soebes.tutorials.neo4jexample.RelationTypes
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

源代码如下:

public class Neo4JApp {

    private String greeting;

    private GraphDatabaseService graphDb;
    private Node firstNode;
    private Node secondNode;
    private Relationship relationship;

    public static void main(final String[] args) {
    Neo4JApp hello = new Neo4JApp();
    hello.createDb(args[0]);
    hello.removeData();
    hello.shutDown();
    }

    void createDb(String path) {
    clearDb(path);

    graphDb = new EmbeddedGraphDatabase(path);
    registerShutdownHook(graphDb);

    Transaction tx = graphDb.beginTx();
    try {

        firstNode = graphDb.createNode();
        firstNode.setProperty("message", "Hello, ");
        secondNode = graphDb.createNode();
        secondNode.setProperty("message", "World!");

        relationship = firstNode.createRelationshipTo(secondNode, RelationTypes.KNOWS);
        relationship.setProperty("message", "brave Neo4j ");

        System.out.print(firstNode.getProperty("message"));
        System.out.print(relationship.getProperty("message"));
        System.out.print(secondNode.getProperty("message"));

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

        tx.success();
    } finally {
        tx.finish();
    }
    }

    private void clearDb(String path) {
    try {
        FileUtils.deleteRecursively(new File(path));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    }

    void removeData() {
    Transaction tx = graphDb.beginTx();
    try {
        firstNode.getSingleRelationship(RelationTypes.KNOWS, Direction.OUTGOING).delete();
        firstNode.delete();
        secondNode.delete();

        tx.success();
    } finally {
        tx.finish();
    }
    }

    void shutDown() {
    System.out.println();
    System.out.println("Shutting down database ...");
    graphDb.shutdown();
    }

    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();
        }
    });
    }

使用的类RelationTypes如下所示:

public enum RelationTypes implements RelationshipType {
    KNOWS, 
    WHOKNOWS,
}

该项目可在github找到。

目前我没有看到这个问题?...在​​正确的位置定义了RelationTypes ...可能有人对我有提示吗?

1 个答案:

答案 0 :(得分:0)

问题是单元测试的目录,不允许作为目标文件夹本身。它必须是目标下的单独文件夹。