java构建工具maven和jar文件

标签 java maven compilation runtime-error

我是 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>   

最佳答案

您缺少 curator-recipes JAR(又名依赖项)。一般是带包装的POM jar将生成一个适合包含在其他项目中的 jar,因此它不适合独立运行。

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

将此添加到您的 <build />节点:

<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() 在哪里方法。

关于java构建工具maven和jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33379667/

相关文章:

java - 替换 Java 字符串中的 '\' 字符

java - 如何在windows下运行jstat?

java - 如何使用 Maven 安装 Slick2d?

java - 如何使用递归方法检查字符串?

java - 设置 Titan 和 Cassandra - 无法实例化存储管理器类

maven - 如何通过maven从命令行输入动态运行testng xml文件列表?

java - AspectJ 插件 Aspect Config 与 Maven 使用外部 Jar for Aspect

Scala(SBT)编译错误: separate output paths (production,测试)

swift - 为什么使用结构会增加应用程序的二进制大小?

c++ - 包含的 C++/C 头文件的实现在哪里?