In Maven, my dependencies are set up but I get a ClassNotFoundException when I run generated jar

后端 未结 2 1704
闹比i
闹比i 2020-12-22 06:29

I was following along with the maven tutorial at http://www.youtube.com/watch?v=IRKu8_l5YiQ.

I created a maven project with the default archetype. Then I added the

相关标签:
2条回答
  • 2020-12-22 07:29

    Consider using maven shade plugin to create an Uber Jar.

    When using slf4j-api you need to add an API implementation dependency at runtime, e.g. slf4j-simple or logback:

    ...
    <properties>
        <slf4j.version>1.7.7</slf4j.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
    <dependencies>
    
    0 讨论(0)
  • 2020-12-22 07:30

    When I first started using Maven, I had to provide some additional build "plugin"/"configuration" in order for the Jar to be built with the correct manifest file, including the class-path entry and the dependent jars included with the jar...

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- This is the prefix for the class path -->
                            <!-- The dependent jars will copied to the lib directory -->
                            <!-- under the main jar -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--mainClass>com.acme.MainClass</mainClass-->
                        </manifest>
                    </archive>
                </configuration>
            </plugin>        
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
         <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
           <!--source>1.7</source-->
           <!--target>1.7</target-->
          </configuration>
         </plugin>
        </plugins>
    </build>
    

    You might want to do some additional reading on some the flags/plugins. This is nearly a year old, so some may have been deprecated...

    0 讨论(0)
提交回复
热议问题