[英]A simple Jena example not working
I am trying to run the first example that comes with the official Jena documentation.我正在尝试运行官方 Jena 文档附带的第一个示例。
Here is the error I am getting when I run the command "java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar":这是我在运行命令“java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar”时遇到的错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mycompany.app.RdfWriter.main(RdfWriter.java:13)
Caused by: java.lang.NullPointerException
at org.apache.jena.tdb.sys.EnvTDB.processGlobalSystemProperties(EnvTDB.java:33)
at org.apache.jena.tdb.TDB.init(TDB.java:248)
at org.apache.jena.tdb.sys.InitTDB.start(InitTDB.java:29)
at org.apache.jena.system.JenaSystem.lambda$init$2(JenaSystem.java:119)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:194)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:171)
at org.apache.jena.system.JenaSystem.init(JenaSystem.java:117)
at org.apache.jena.rdf.model.ModelFactory.<clinit>(ModelFactory.java:49)
... 1 more
Here is my pom file:这是我的 pom 文件:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<version>3.1.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.app.RdfWriter</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is the only file that I have so far:这是我迄今为止唯一的文件:
package com.mycompany.app;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.*;
public class RdfWriter {
static String personURI = "http://somewhere/JohnSmith";
static String fullName = "John Smith";
public static void main(String[] args) {
// create an empty model
Model model = ModelFactory.createDefaultModel();
// create the resource
Resource johnSmith = model.createResource(personURI);
// add the property
johnSmith.addProperty(VCARD.FN, fullName);
System.out.println("this is rdf writer");
}
}
I only want to get started, so if there is way to fix the pom or even not using maven if it is simpler, please let me know.我只想开始,所以如果有办法修复 pom 或者甚至不使用 maven,如果它更简单,请告诉我。
I added a new dependency to the dependencies in my pom file after Tahir mentioned that in the answer like this:在 Tahir 在这样的答案中提到之后,我向我的 pom 文件中的依赖项添加了一个新依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.2</version>
</dependency>
And now I am getting this error:现在我收到此错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mycompany.app.RdfWriter.main(RdfWriter.java:14)
Caused by: java.lang.NullPointerException
at org.apache.jena.tdb.sys.EnvTDB.processGlobalSystemProperties(EnvTDB.java:33)
at org.apache.jena.tdb.TDB.init(TDB.java:248)
at org.apache.jena.tdb.sys.InitTDB.start(InitTDB.java:29)
at org.apache.jena.system.JenaSystem.lambda$init$2(JenaSystem.java:119)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:194)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:171)
at org.apache.jena.system.JenaSystem.init(JenaSystem.java:117)
at org.apache.jena.rdf.model.ModelFactory.<clinit>(ModelFactory.java:49)
... 1 more
Thanks谢谢
When you repacked the jar with the assembly plugin, the ServiceLoader
files got mangled.当您使用程序集插件重新打包 jar 时, ServiceLoader
文件被破坏了。 ServiceLoader
is a standard Java feature Jena uses to provide initialization across jars. ServiceLoader
是 Jena 用来提供跨 jar 初始化的标准 Java 功能。
You need to combine all the files called您需要组合所有名为
META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle
from the jars (there are several of these files, different contents).来自罐子(有几个这样的文件,不同的内容)。
If you really must build a combined jar, rather than say use "mvn exec:exec", then use the shade plugin with <transformer implementation= "org.apache.maven.plugins.shade.resource.ServicesResourceTransformer/>
.如果你真的必须构建一个组合 jar,而不是说使用“mvn exec:exec”,那么使用带有<transformer implementation= "org.apache.maven.plugins.shade.resource.ServicesResourceTransformer/>
的 shade 插件。
You need to add following jar file in your classpath:您需要在类路径中添加以下 jar 文件:
slf4j-simple-1.6.1.jar slf4j-simple-1.6.1.jar
Just add this dependency and see whether it will solve your problrm or not?只需添加此依赖项,看看它是否会解决您的问题?
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
The instructions found here:http://www.iandickinson.me.uk/articles/jena-eclipse-helloworld/ worked for me.此处找到的说明:http: //www.iandickinson.me.uk/articles/jena-eclipse-helloworld/ 对我有用。
I still think it should not be complicated to make a valid pom for this.我仍然认为为此制作一个有效的 pom 不应该很复杂。 If nobody provides a pom that allows this to run without errors, I will accept my own answer.如果没有人提供允许它无错误运行的 pom,我将接受我自己的答案。
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.