3

I am new to JAVA and want to run a sample program from online.

I downloaded a package from github https://github.com/yiming187/curator-example

I compiled it using command mvn package. The result shows 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. 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

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

<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>   
12
  • You should've posted the POM in your question ;-) Anyway, you're missing the curator-recipes JAR (a.k.a the dependency). Generally, a POM with packaging jar will produce a jar suited for being included in other projects. Commented Oct 27, 2015 at 22:43
  • Although, it surprises me that one can get a SUCCESS from mvn package when an artifact is missing. Maybe I don't understand how mvn package works. Commented Oct 27, 2015 at 22:45
  • @watery. how can i fix it? I am really new to Maven either Commented Oct 27, 2015 at 22:50
  • @TJamesBoone the package is only missing when he tries to run the jar from the command line, because that's not the way Maven jars are designed to be used - hence the special assembly directive. Commented Oct 27, 2015 at 22:52
  • To run it from the command-line you presumably need to include all the .jars that you need under the -cp parameter, and the apache curator jar is not there. Try running something like this: java -cp curator-example-0.0.1-SNAPSHOT.jar;curator-recipes-2.7.1.jar com/ctrip/zk/curator/example/DistributedIdQueueExample Commented Oct 27, 2015 at 23:06

2 Answers 2

2

You're missing the curator-recipes JAR (a.k.a the dependency). 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.

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.

Add this in your <build /> node:

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

Sign up to request clarification or add additional context in comments.

5 Comments

there is a main method in each sample. if I want to try them one by one. how can i do?
From what I can tell, you can't. As I know it, only one main class can be defined per jar by its MANIFEST file (I may be wrong though); your best bet is, IMHO, to explicitly call each main method from the command line, disregarding the <archive /> node I suggested.
@cppython It seems like this sample project was designed to have its main methods run in an IDE like eclipse, rather than with a command line.
I have eclipse. how to do it?
it works with this command: java -cp curator-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar com/ctrip/zk/curator/example/DistributedIdQueueExample
1

It's very odd that the curator example pom does not include the curator-framework jar. Because the missing classes are not in the curator-recipes jar, they're in the curator-framework jar. I suppose the recipe jar pulls in the framework jar as a dependency.

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.

3 Comments

i think the pom is missing a lot of dependencys. let me add them one by one
from eclipse, I see curator-recipes is depended on curator-framework. does maven solve dependency automatically?
Yes, maven can pull in dependencies automatically. If you run mvn dependency:tree you can see all the dependencies that maven has pulled in. You probably only need the curator-recipes dependency after all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.