【问题标题】:java build tool maven and jar filejava构建工具maven和jar文件
【发布时间】:2016-01-27 13:18:30
【问题描述】:

我是 JAVA 新手,想从网上运行一个示例程序。

我从github下载了一个包https://github.com/yiming187/curator-example

我使用命令mvn package 编译它。 结果显示BUILD SUCCESS。

    [vagrant@bb720864d128 curator-example]$ mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building curator-example 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ curator-example ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /vagrant/curator-example/src/main/resources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ curator-example ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ curator-example ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /vagrant/curator-example/src/test/resources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ curator-example ---
    [INFO] No sources to compile
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ curator-example --

-
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ curator-example ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.767s
[INFO] Finished at: Tue Oct 27 22:27:29 UTC 2015
[INFO] Final Memory: 8M/237M
[INFO] ------------------------------------------------------------------------

然后我转到目标文件并找到 curator-example-0.0.1-SNAPSHOT.jar。我试图为它运行一个示例。但它不起作用。

java -cp curator-example-0.0.1-SNAPSHOT.jar com/ctrip/zk/curator/example/DistributedIdQueueExample

输出:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        CuratorFramework cannot be resolved to a type
        DistributedIdQueue cannot be resolved to a type
        CuratorFrameworkFactory cannot be resolved
        ExponentialBackoffRetry cannot be resolved to a type
        CuratorListener cannot be resolved to a type
        CuratorFramework cannot be resolved to a type
        CuratorEvent cannot be resolved to a type
        QueueConsumer cannot be resolved to a type
        The method createQueueConsumer() from the type DistributedIdQueueExample refers to the missing type QueueConsumer
        QueueBuilder cannot be resolved to a type
        QueueBuilder cannot be resolved
        The method createQueueSerializer() from the type DistributedIdQueueExample refers to the missing type QueueSerializer
        CloseableUtils cannot be resolved
        CloseableUtils cannot be resolved

        at com.ctrip.zk.curator.example.DistributedIdQueueExample.main(DistributedIdQueueExample.java:20)

pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ctrip.zk</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <artifactId>curator-example</artifactId>
    <name>curator-example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.7.1</version>
        </dependency>
    </dependencies>

</project>   

【问题讨论】:

  • 你应该在你的问题中发布 POM ;-) 无论如何,你错过了 curator-recipes JAR(也就是依赖项)。一般来说,带有jar 包装的 POM 会生成一个适合包含在其他项目中的 jar。
  • 虽然,令我惊讶的是,当工件丢失时,可以从 mvn 包中获得 SUCCESS。也许我不明白 mvn package 是如何工作的。
  • @watery。我该如何解决?我对 Maven 也很陌生
  • @TJamesBoone 仅当他尝试从命令行运行 jar 时,该包才丢失,因为这不是 Maven jar 的设计方式 - 因此特殊的汇编指令。
  • 要从命令行运行它,您可能需要在 -cp 参数下包含您需要的所有 .jar,并且 apache curator jar 不存在。尝试运行这样的东西:java -cp curator-example-0.0.1-SNAPSHOT.jar;curator-recipes-2.7.1.jar com/ctrip/zk/curator/example/DistributedIdQueueExample

标签: java maven compilation runtime-error


【解决方案1】:

您缺少 curator-recipes JAR(也称为依赖项)。一般来说,打包jar的POM会产生一个适合包含在其他项目中的jar,所以不适合单机运行。

如果你想从命令行运行它,你需要一个所谓的“jar-with-dependencies”,一个特殊的包装,所有的依赖(直接和传递)都包含在你的最终工件中。

将此添加到您的 &lt;build /&gt; 节点中:

<plugins>

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <!--
            <archive>
                <manifest>
                    <mainClass>package.and.name.of.main.class</mainClass>
                </manifest>
            </archive>
            -->
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

</plugins>

请注意,我注释掉了 &lt;archive /&gt; 部分,供参考。 如果你真的有一个主类,你可以在那里指定它,然后你可以用java -jar &lt;jar-file&gt;运行你的项目,MANIFEST会告诉Javamain()方法在哪里。

【讨论】:

  • 每个样本都有一个主要的方法。如果我想一一尝试。我该怎么办?
  • 据我所知,你不能。据我所知,每个 jar 的 MANIFEST 文件只能定义一个主类(不过我可能错了);恕我直言,您最好的选择是从命令行显式调用每个主要方法,而忽略我建议的 &lt;archive /&gt; 节点。
  • @cppython 看起来这个示例项目的设计目的是让它的主要方法在 Eclipse 这样的 IDE 中运行,而不是在命令行中运行。
  • 我有日食。怎么办?
  • 它使用这个命令:java -cp curator-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar com/ctrip/zk/curator/example/DistributedIdQueueExample
【解决方案2】:

很奇怪,curator 示例 pom 不包含 curator-framework jar。因为缺少的类不在 curator-recipes jar 中,所以它们在 curator-framework jar 中。我想配方 jar 将框架 jar 作为依赖项引入。

尝试将依赖项更改为以下内容,然后重新编译:

   <dependencies>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.7.1</version>
    </dependency>
</dependencies>

然后运行我放在 cmets 中的最后一个 java 命令。

【讨论】:

  • 我认为 pom 缺少很多依赖项。让我一一添加
  • 来自 eclipse,我看到 curator-recipes 依赖于 curator-framework。 maven会自动解决依赖吗?
  • 是的,maven 可以自动引入依赖。如果你运行 mvn dependency:tree 你可以看到 maven 引入的所有依赖项。毕竟你可能只需要 curator-recipes 依赖项。
猜你喜欢
  • 1970-01-01
  • 2011-05-14
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2019-11-28
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多