简体   繁体   English

java构建工具maven和jar文件

[英]java build tool maven and jar file

I am new to JAVA and want to run a sample program from online. 我是JAVA的新手,想从网上运行一个示例程序。

I downloaded a package from github https://github.com/yiming187/curator-example 我从github https://github.com/yiming187/curator-example下载了一个包

I compiled it using command mvn package . 我用命令mvn package编译它。 The result shows BUILD SUCCESS. 结果显示了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] ------------------------------------------------------------------------

I then went to a target file and found curator-example-0.0.1-SNAPSHOT.jar. 然后我去了一个目标文件,找到了curator-example-0.0.1-SNAPSHOT.jar。 I tried to run one of the examples for it. 我试着为它运行一个例子。 But it doesn't work. 但它不起作用。

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

output: 输出:

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 的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>   

You're missing the curator-recipes JAR (aka the dependency). 你错过了策展人食谱JAR(又名依赖)。 Generally, a POM with packaging jar will produce a jar suited for being included in other projects, so it is not suited for standalone running. 通常,带有包装jar的POM将生产适合包含在其他项目中的罐子,因此它不适合独立运行。

If you want to run it from the command line, you need a so-called "jar-with-dependencies", a special packaging where all the dependencies (both direct and transitive) are included in your final artifact. 如果要从命令行运行它,则需要一个所谓的“jar-with-dependencies”,这是一个特殊的包装,其中所有依赖项(直接和传递)都包含在最终工件中。

Add this in your <build /> node: <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>

Note that I commented out the <archive /> part, that's there for reference. 请注意,我注释掉了<archive />部分,可供参考。 If you really have a main class, you can specify it there, and then you can run your project with java -jar <jar-file> , the MANIFEST will tell Java where's the main() method. 如果你真的有一个主类,你可以在那里指定它,然后你可以用java -jar <jar-file>运行你的项目,MANIFEST会告诉Java哪里是main()方法。

It's very odd that the curator example pom does not include the curator-framework jar. 策展人示例pom不包括策展人框架jar,这是非常奇怪的。 Because the missing classes are not in the curator-recipes jar, they're in the curator-framework jar. 因为缺少的类不在curator-recipes jar中,所以它们在curator-framework jar中。 I suppose the recipe jar pulls in the framework jar as a dependency. 我想配方jar作为依赖项拉入框架jar。

Try changing the dependencies to the following and then recompile: 尝试将依赖项更改为以下内容,然后重新编译:

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

Then run the last java command I put in the comments. 然后运行我在评论中放入的最后一个java命令。

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

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