gpt4 book ai didi

java - Jena Fuseki 客户端

转载 作者:行者123 更新时间:2023-12-02 03:17:58 24 4
gpt4 key购买 nike

我正在尝试用 Java 编写一个简单的 Jena Fuseki 客户端来发送 SELECT SPARQL 查询并获取结果。

我发现下面的代码是别人写的。它在运行时不断崩溃。

我正在使用jena-arq-3.1.0.jar

javac -cp .;jena-arq-3.1.0.jar Main.java 打印无错误

java -cp .;jena-arq-3.1.0.jar Main 崩溃!

错误日志

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/atlas/io/Printable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:78)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:52)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:40)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.atlas.io.Printable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more

Main.java

import java.net.URL;
import java.util.List;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;

class Main {

public static void main(final String[] args) throws Exception {
String queryString=
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"prefix owl: <http://www.w3.org/2002/07/owl#>"+

"SELECT ?subject ?predicate ?object"+
"WHERE {"+
" ?subject ?predicate ?object"+
" } LIMIT 25";

// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:3030/FirstEndpoint/sparql", query);

//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {

// Result processing is done here.
}
}
finally {
qexec.close();
}
}

}

最佳答案

您需要类路径中的所有必需库,而不仅仅是 jena-arq

javac 之所以有效,是因为您仅使用 jena-arq JAR 中的类。java 不起作用,因为在运行时 ARQ 的类引用其他类,例如包含在 jena-core、jena-iri 等中

最简单的方法是添加 Jena 发行版中的所有库。将 lib 文件夹本身添加到类路径中。

关于java - Jena Fuseki 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com