简体   繁体   English

从命令行使用 Json 执行 maven 项目

[英]execute maven project with Json from command line

I am starting a project to read and analyze a JSON-file with JAVA 8. To have it run in Eclipse, I turned it into a maven project and added this dependency :我正在启动一个项目来使用 JAVA 8 读取和分析 JSON 文件。为了让它在 Eclipse 中运行,我将它变成了一个 maven 项目并添加了这个依赖项:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1</version>
    <scope>runtime</scope>
</dependency>

Within Eclipse , there is no problem, but when I run it from command line, I get this error:在 Eclipse 中,没有问题,但是当我从命令行运行它时,出现此错误:

Provider org.glassfish.json.JsonProviderImpl not found未找到提供程序 org.glassfish.json.JsonProviderImpl

In futur I want to run it on a server without an Eclipse installation.将来我想在没有 Eclipse 安装的服务器上运行它。 How can I get it running ?我怎样才能让它运行?

Please see this question .请看这个问题 It will help you create a jar file that has all the dependencies built into it.它将帮助您创建一个内置所有依赖项的 jar 文件。 I would not suggest rewriting things and to use libraries freely.我不建议重写东西并自由使用库。 You code much faster and in more bug free ways when you do not code it yourself.当您不自己编码时,您的编码速度会更快,并且不会出现错误。

Once you have done this you will be able to run java -jar on the jar file and your application will run.完成此操作后,您将能够在 jar 文件上运行 java -jar 并且您的应用程序将运行。 If you would like to just have the thing run you can download the jar and add it to the classpath variable that you pass the java command line.如果您只想运行该东西,您可以下载 jar 并将其添加到您通过 java 命令行传递的类路径变量中。

thanks a lot.多谢。 Following maven configuration gets it work for command line :以下 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>

if you want simply deserialize using Gson.如果您只想使用 Gson 进行反序列化。 you use this sample program你使用这个示例程序

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));
    }
}

you will need following dependencies您将需要以下依赖项

    <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM