Why dependencies do not accompany the made package by Maven?

前端 未结 3 1979
悲哀的现实
悲哀的现实 2021-01-27 06:40

I\'ve followed the simplest maven example and made the following pom.xml file:



        
相关标签:
3条回答
  • 2021-01-27 07:19

    As per exception ClassNotFoundException: org.json.simple.parser.JSONParser the required jar file is not present in classpath.

    In eclipse use below steps:

        Right click on project --> properties --> deployment assembly --> add 
             --> (here select appropriate file system to add jar like)java build path entries
    

    hope it help.

    0 讨论(0)
  • 2021-01-27 07:35

    By default, jar doesn't include dependencies within it. You either need to add all dependencies in classpath at run time or you can build jar with dependencies. For building jar with dependencies see link: http://maven.apache.org/plugins/maven-assembly-plugin/

    0 讨论(0)
  • 2021-01-27 07:40

    The problem is, you are setting the classpath to a single .jar file, thus java command can not find the dependency. (json-simple-1.1.1.jar in your case..)

    You can tell maven to include the dependencies in target folder that you compiling your class to.

    This is done by the following change in the pom.xml:

    <build>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <executions>
            <execution>
              <phase>install</phase>
              <goals>
                <goal>copy-dependencies</goal>
              </goals>
              <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    

    Now try mvn package (or mvn clean install, I am not sure..) and you will see in /target/lib the dependencies are copied.

    Run your application like this:

    java -cp target/my-app-1.0-SNAPSHOT.jar:target/lib/json-simple-1.1.1.jar com.mycompany.app.App
    {"hello":"world"}
    
    0 讨论(0)
提交回复
热议问题