gpt4 book ai didi

java构建工具maven和jar文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:07 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com