我这个程序的目标是通过java运行Sparql查询,并在控制台中获得相应的结果。
如果我没有错的话,这个错误是因为缺少一个jar文件。然而,在我看来,已经添加了所需的jar文件。我添加了以下jar文件:Jena-2.1.jar
和arq-2.8.7.jar
。
我的代码如下:
public class Example {
public static void main(String[] args) {
//http request
String website = "http://dbpedia.org/sparql";
//whatever query we want to parse.
String query = "SELECT ?abstract WHERE { { <http://dbpedia.org/resource/Ronaldo> <http://dbpedia.org/ontology/abstract> ?abstract } }";
//query execution using the library object.
QueryExecution queryEx = QueryExecutionFactory.sparqlService(website, query);
try {
//results coming from the executed queries.
ResultSet results = queryEx.execSelect();
//prints the output.
for(; results.hasNext();){
//typecast results from set to qsolution
QuerySolution answer = (QuerySolution)results.next();
System.out.println(answer.get("?abstract"));
}
} catch(Exception e){
e.printStackTrace();
} finally{
queryEx.close();
}
}
我收到的错误图像是:
<pre>Exception in thread "main" java.lang.NoSuchMethodError: com.hp.hpl.jena.iri.IRIFactory.jenaImplementation()Lcom/hp/hpl/jena/iri/IRIFactory;
at com.hp.hpl.jena.n3.IRIResolver.<clinit>(IRIResolver.java:42)
at com.hp.hpl.jena.sparql.mgt.SystemInfo.createIRI(SystemInfo.java:31)
at com.hp.hpl.jena.sparql.mgt.SystemInfo.<init>(SystemInfo.java:23)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:373)
at com.hp.hpl.jena.query.ARQ.<clinit>(ARQ.java:385)
at com.hp.hpl.jena.query.Query.<clinit>(Query.java:53)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:68)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:28)
at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:338)
at Example.main(Example.java:27)<code>
如有任何指导,我们将不胜感激。提前谢谢。
jena-2.1是2004年2月。它与使用Jena 2.6.4的arq-2.8.7.jar(2010)不兼容。
现在是2016年。有关当前版本,请参阅:http://jena.apache.org/download/
对于任何jena版本(当前版本为3.1.0),都应该使用发行版中提供的所有jar。更好的是,就像在评论中一样,学会使用use maven/gradle/。。。这将自动发生在你身上。
不支持混合不同版本的部分罐子,而且极不可能工作。