[英]execute maven project with Json from command line
我正在啟動一個項目來使用 JAVA 8 讀取和分析 JSON 文件。為了讓它在 Eclipse 中運行,我將它變成了一個 maven 項目並添加了這個依賴項:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
<scope>runtime</scope>
</dependency>
在 Eclipse 中,沒有問題,但是當我從命令行運行它時,出現此錯誤:
未找到提供程序 org.glassfish.json.JsonProviderImpl
將來我想在沒有 Eclipse 安裝的服務器上運行它。 我怎樣才能讓它運行?
請看這個問題。 它將幫助您創建一個內置所有依賴項的 jar 文件。 我不建議重寫東西並自由使用庫。 當您不自己編碼時,您的編碼速度會更快,並且不會出現錯誤。
完成此操作后,您將能夠在 jar 文件上運行 java -jar 並且您的應用程序將運行。 如果您只想運行該東西,您可以下載 jar 並將其添加到您通過 java 命令行傳遞的類路徑變量中。
多謝。 以下 maven 配置使其適用於命令行:
<plugins>
..
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>data.Parser</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
如果您只想使用 Gson 進行反序列化。 你使用這個示例程序
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) throws IOException {
String json = FileUtils.readFileToString(new File("PATH_TO_JSON"), "UTF-8");
Gson deserializer = new Gson();
System.out.println(deserializer.fromJson(json, Map.class));
}
}
您將需要以下依賴項
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.