Java构建工具Maven和Jar文件

3

我是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文件。 - watery
尽管我很惊讶,当一个构件丢失时,mvn package仍然可以获得SUCCESS。也许我不理解mvn package的工作原理。 - James Dunn
要从命令行运行它,您可能需要在-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 - mwarren
将两个jar包之间的分号;更改为冒号:,换句话说,java -cp curator-example-0.0.1-SNAPSHOT.jar:curator-recipes-2.7.1.jar com/ctrip/zk/curator/example/DistributedIdQueueExample - mwarren
我已将所有的jar包下载到目标文件中:curator-client-2.7.1.jar、curator-recipes-2.7.1.jar、json-simple-1.1.1.jar、curator-example-0.0.1-SNAPSHOT.jar、curator-framework-2.7.1.jar和zookeeper-3.4.6.jar。 - cppython
显示剩余7条评论
2个回答

2
您缺少curator-recipes JAR(即依赖项)。通常,使用包装jar的POM将生成适合包含在其他项目中的jar,因此不适合独立运行。
如果您想从命令行运行它,则需要所谓的“带依赖项的jar”,这是一种特殊的打包方式,其中所有依赖项(直接和传递)都包含在最终构件中。
请将以下内容添加到您的节点中:
<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>

请注意,我注释掉了<archive />部分,这只是为了提供参考。如果你确实有一个主类,可以在那里指定,然后可以使用java -jar <jar-file>运行你的项目,MANIFEST会告诉Java main()方法在哪里。

每个示例中都有一个主方法。如果我想一个接一个地尝试它们,我该怎么做? - cppython
据我所知,你不能这样做。在我的了解中,每个JAR文件的MANIFEST文件只能定义一个主类(尽管我可能是错的);我认为最好的方法是从命令行显式地调用每个主方法,而忽略我建议的<archive />节点。 - watery
@cppython 看起来这个示例项目是设计为在像eclipse这样的IDE中运行其主要方法,而不是通过命令行运行。 - James Dunn
我有Eclipse。怎么做? - cppython
它可以通过以下命令运行:java -cp curator-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar com/ctrip/zk/curator/example/DistributedIdQueueExample - cppython

1
很奇怪,curator示例pom文件中不包含curator-framework jar文件。因为缺少的类不在curator-recipes jar文件中,而是在curator-framework jar文件中。我猜测recipe jar文件将framework 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>

请运行我在评论中提供的最后一个Java命令。

我认为POM文件缺少很多依赖项。让我逐个添加它们。 - cppython
从Eclipse中,我看到Curator-recipes依赖于Curator-framework。Maven会自动解决依赖关系吗? - cppython
是的,Maven 可以自动拉取依赖项。如果运行 mvn dependency:tree 命令,您可以查看 Maven 拉取的所有依赖项。毕竟,您可能只需要 curator-recipes 依赖项。 - mwarren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接