maven 无法初始化jar中的主类

5m1hhzi4  于 2023-10-17  发布在  Maven
关注(0)|答案(1)|浏览(212)

我需要创建一个可以发射的jar。为此,我在插件的pom中添加了maven-jar-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.1.0</version>
  <configuration>                   
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>main.MyClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

当我在命令行上运行这个jar时,
C:\Users\user\git\myfolder\target>java -jar myprojectname-0.1.0-SNAPSHOT.jar错误:无法初始化主类main.MyClass原因:java.lang.NoClassDefFoundError:org/openqa/selenium/NoSuchElementException
如果我通过Eclipse导出一个jar,指定了主类,那么在清单中,主类被指示为main.MyClass,在这种情况下,jar被启动。
pom的全部内容

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    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.user</groupId>
    <artifactId>myprojectname</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.java.version>1.8</project.java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${project.java.version}</source>
                    <target>${project.java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>

                    <excludes>
                        <exclude>setup/*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>                 
                    <archive>
                        <manifest>
                            <mainClass>main.MyClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

    </build>
</project>

怎么修?

jk9hmnmh

jk9hmnmh1#

你可以像这样构建bigjar,让你的jar包含其他依赖项:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>org.krzywanski.table.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

完整的pom你可以在这里找到:Working pom
或者在其他目录中包含依赖项,如下所示:

<build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <archive>
                    <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>my.Main</mainClass>
                    </manifest>
                </archive>
                <outputDirectory>${project.parent.basedir}</outputDirectory>
            </configuration>

        </plugin>
    </plugins>
</build>

并将依赖项放在pom

下的“lib”目录中

相关问题