简体   繁体   English

使用命令行中的参数从JAR运行Maven项目

[英]Run maven project from JAR with arguments from command line

Hi i Have a maven project used three libraries: junit, commons-lang, org.apache.commons My program is located in location pl.parser.example.MainClass (with main method) and three others : Currency.java, CurrencyPojo.java, CurrencyRates.java 嗨,我有一个maven项目使用了三个库:junit,commons-lang,org.apache.commons。我的程序位于pl.parser.example.MainClass(使用main方法)位置;另外三个位于:Currency.java,CurrencyPojo.java ,CurrencyRates.java

My main method: 我的主要方法:

 public static void main(String[] args){
  try {
    String type = "c";

    String currencyCode = args[0];
    String inputStartDate = args[1];
    String inputEndDate = args[2];
...

I can't run this program from command line, i build it with maven to jar file and try: 我无法从命令行运行此程序,我使用maven将其构建为jar文件并尝试:

java -cp AreYouExample.jar pl.parser.example.MainClass EUR 2013-01-28 2013-01-31

But i got exception: 但我有例外:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ma
th3/stat/descriptive/DescriptiveStatistics
        at pl.parser.example.CurrencyRates.obtainAvgPurchaseRate(
CurrencyRates.java:33)
        at pl.parser.example.MainClass.main(MainClass.java:34)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.descr
iptive.DescriptiveStatistics
        at java.net.URLClassLoader$1.run(Unknown Source)...

I think that runner can't see library which i used in program (to get averange count from math) 我认为跑步者看不到我在程序中使用的库(从数学获取平均数)

I don'thave a manifest file, and classpath file. 我没有清单文件和类路径文件。 When I tried run jar with: 当我尝试运行jar时:

    ...> java MainClass EUR 2013-01-28 2013-01-31

Then i got exception: 然后我得到了例外:

Error: Could not find or load main class MainClass.java

But when i will go to folder with MainClass.java and run javaC MainClass.java... Then compiller cant find symbols used in program because sumbols are located in other class (in this folder/package) 但是当我将要与MainClass.java一起转到文件夹并运行javaC MainClass.java时...编译器无法找到程序中使用的符号,因为sumbols位于其他类中(在此文件夹/包中)

I think that i should insert a main class i suggested this case in stack overflow, build in pom file but that not works. 我认为我应该在堆栈溢出中插入我建议的这种情况下的主类, 在pom文件中进行构建,但这不起作用。

EDIT: Now it works, i made a change in POM file like in other overstackflow topic I insertet this code: 编辑:现在它起作用了,我像在其他overstackflow主题中一样在POM文件中进行了更改,我插入了以下代码:

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.0</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>your.main.Class</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

You're only specifying your jar in the classpath when you're launching the app java -cp AreYouExample.jar so you need to add the apache-commons lib to the classpath as well. 您仅在启动应用程序java -cp AreYouExample.jar时在类路径中指定jar,因此还需要将apache-commons lib添加到类路径中。

Usually when building and preparing your application for distribution, I'd have a lib directory and configure maven copy your dependencies in there, so my startup script can add them all to the classpath and launch the app. 通常,在构建和准备分发应用程序时,我会在其中有一个lib目录并配置maven复制您的依赖项,因此我的启动脚本可以将它们全部添加到类路径中并启动应用程序。

you need a Main class in Manifest file in your jar that has been created by maven. 您需要在jar中由maven创建的Manifest文件中的Main类。

Manifest-Version: 1.0
Main-Class: com.main class name

it clearly is not able to start executing the main program in your jar 它显然无法开始在jar中执行主程序

Error: Could not find or load main class MainClass.java

Create a Manifest file and specify your Main class there. 创建一个清单文件并在其中指定您的Main类。 Then use the classpath option -cp to pass any values to your jar like property file. 然后,使用classpath选项-cp将所有值传递给jar文件,例如属性文件。 All the common library jars will be loaded by maven automatically when you execute the jar from the command line. 当您从命令行执行jar时,所有公共库jar都将由maven自动加载。

If you only run you program without telling Java where all the library classes reside, you will exactly get that NoClassDefFoundError . 如果只运行程序而不告诉Java所有库类都在哪里,那么您将确切地获得NoClassDefFoundError

There are several possibilities to run your program correctly . 有几种方法可以正确运行程序。

  • java -jar YourJarFile.jar

This command requires a manifest file inside the JAR file. 此命令需要JAR文件中的清单文件。 This manifest must include the two properties Main-Class and Class-Path . 此清单必须包含两个属性Main-ClassClass-Path For your program this class path property in the manifest file must contain the libraries that you are using 对于您的程序,清单文件中的此类路径属性必须包含您正在使用的库

  • java -cp <jar-file> your.pkg.MainClass

This command explicitely states the class path in the command, which now must be simply your own JAR file. 该命令在命令中明确声明类路径,该类路径现在必须只是您自己的JAR文件。 Then it also states the main class, whose main method should be run. 然后,它还声明应运行其主方法的主类。 That means that you do not need the Main-Class property anymore in the manifest. 这意味着清单中不再需要Main-Class属性。 But you still need the Class-Path property for finding the used libraries. 但是您仍然需要Class-Path属性来查找使用的库。

  • java -cp <jar-file>;<lib1>;<lib2>;... your.pkg.MainClass

With this command you do not need any explicit manifest anymore. 使用此命令,您不再需要任何显式清单。 Simply put your own JAR file and the libraries into the excplicit class path (inside the comman). 只需将您自己的JAR文件和库放入专有类路径(在comman内部)。


With Maven you are able to tell the archiver how to create the manifest. 使用Maven,您可以告诉归档程序如何创建清单。 Additionally, you can have Maven create the whole distribution. 另外,您可以让Maven创建整个发行版。 Have a look at the assembly plugin . 看一下Assembly插件

I had a similar problem some time ago. 我前段时间也遇到过类似的问题。 If you run your maven from eclipse and you haven't defined your source folder in your project then your eclipse maven plugin won't know what to compile, so no classes will be added to jar(check your maven logs and see it adds your classes). 如果您从eclipse运行maven并且未在项目中定义源文件夹,则eclipse maven插件将不知道要编译什么,因此不会将任何类添加到jar(检查您的maven日志并查看是否添加了类)。 So you should assure yourself that src/main/java is added to eclipse project. 因此,您应该向自己保证src / main / java已添加到eclipse项目中。 Check your project properties build path. 检查项目属性的构建路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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