u011856283
你好杰米
采纳率95%
2019-01-11 20:29

用maven打了一个可运行jar,但是运行提示找不到类

5
<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>oms</groupId>
  <artifactId>international</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>

  <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
        <scope>compile</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.54</version>
        <scope>compile</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>




  </dependencies>

  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <archive>
                        <manifest>
                            <mainClass>oms.window.Windowtest</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
                </configuration>
          </plugin>

        </plugins>
    </pluginManagement>
  </build>
</project>

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • tangruize trzzzZ 2年前

    您好! 您只使用了maven-jar-plugin对项目代码进行打包, 但没有将依赖关系打包进去, 可以添加maven-dependency-plugin.

    也可以尝试一下使用下面的插件(并去掉你原来的).

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2.1</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    顺便说一下, 如果你使用当前的做法, 并将依赖关系的jar包放在classpath下, 则也能运行.

    另外, 使用我提供的插件, 依赖关系如果为 <scope>provided</scope>则这个依赖关系不会被打包进去, 方便精准控制.

    望采纳.

    点赞 4 评论 复制链接分享