在尝试用JPyp调用Javaclasses(Neo4j)时会出现很多错误(异常)

2024-05-14 10:12:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Java和Python桥JPype,但也尝试使用Neo4j(这是一个图形数据库)。当我尝试用JPYPE运行一个简单的Java程序时,没有问题。你知道吗

import jpype as jp

jp.startJVM(jp.getDefaultJVMPath(), "-ea")
javaClass = jp.JClass('Jpype_test.A2')
javaInstance = javaClass()
javaInstance.callMe(2, 3)
jp.shutdownJVM()

Java中的类只是:

package Jpype_test;

import helloworld.EmbeddedNeo4j;

import java.io.IOException;

public class A2 {
    public A2() {
        super();

    }

    public String callMe(final int a, final int b) throws IOException {
        final int res = Math.abs(a - b);
        try {
            EmbeddedNeo4j.main(null);
        } finally {
            System.out.println("I can't do this!");
        }

        return ("Hello, World!" + res);
    }
}

但是当我尝试运行包括Neo4j在内的“同一个”HelloWorld程序时,会出现很多错误,我不知道自己做错了什么。你知道吗

package helloworld;

import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;

public class helloworld {

private static final String db_path = "target/neo4j-hello-db";

public String greeting;

GraphDatabaseService graphdb;
Node firstNode;
Node secondNode;
Relationship rs;

private static enum RelTypes implements RelationshipType {
    KNOWS
}

public static void main(final String[] args) throws IOException {
    final helloworld hello = new helloworld();
    hello.createDb();
    hello.removeData();
    hello.shutDown();

}

void createDb() throws IOException {
    FileUtils.deleteRecursively(new File(db_path));
    graphdb = new GraphDatabaseFactory().newEmbeddedDatabase(db_path);
    registerShutdownHook(graphdb);

    try (Transaction tx = graphdb.beginTx()) {
        firstNode = graphdb.createNode();
        firstNode.setProperty("message", "Hello, ");
        secondNode = graphdb.createNode();
        secondNode.setProperty("message", "World!");

        rs = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS);
        rs.setProperty("message", "brave Neo");

        System.out.println(firstNode.getProperty("message"));
        System.out.println(rs.getProperty("message"));
        System.out.println(secondNode.getProperty("message"));

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

        tx.success();

    }
}

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

        tx.success();
    }
}

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

private static void registerShutdownHook(final GraphDatabaseService graphdb) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            graphdb.shutdown();
        }
    });
}

}

>> Traceback (most recent call last):
    File "C:\Projects\Argon\workspace\TestNeo4jProject\src\helloworld\file.py",   line 6, in <module>
    javaClass = jp.JClass('helloworld.Hello')
  File "C:\Python27\lib\site-packages\jpype\_jclass.py", line 54, in JClass
    raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.ExceptionPyRaisable: java.lang.Exception: Class helloworld.Hello not found

我想类路径或类似的东西可能有问题,但我觉得很奇怪。。如果有人能帮忙解决问题,那就太感谢了!你知道吗


Tags: orgimportmessagestringpublicoutsystemhelloworld
1条回答
网友
1楼 · 发布于 2024-05-14 10:12:53

异常状态:Class helloworld.Hello not found。您的类实际上被命名为helloworld.helloworld。因此,要么确保调用的类名正确,要么更改类名,使其为helloworld.Hello。你知道吗

相关问题 更多 >

    热门问题