如何运行使用maven创建的可执行jar文件?

时间:2022-04-21 17:14:21

We are working on Automation Testing which uses selenium and Junit . Our requirement is to make an executable jar file using Maven. Project Structure is look like this

我们正在开发使用selenium和Junit的自动化测试。我们的要求是使用Maven制作可执行的jar文件。项目结构看起来像这样

  Project 
   |src\main\java
   |src\main\res
   |src\test\java
       |testmain
           |MainTest.java
   |src\test\res
   |target
   |pom.xml

We are using maven assembly plugin to create jar file

我们正在使用maven程序集插件来创建jar文件

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>testMain.MainTest</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>`

we can able to create jar file(SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar) using this command .

我们可以使用此命令创建jar文件(SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar)。

clean package assembly:single

清洁包装配:单个

But when we try to run it using

但是当我们尝试使用它时

java -jar SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar

java -jar SampleMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar

we are getting this error

我们收到了这个错误

Could not find or load main class testMain.MainTest

无法找到或加载主类testMain.MainTest

Please help me to resolve this error

请帮我解决这个错误

EDIT:

As i mentioned above ,i couldn't have MainTest.java in src/main folder since our project is only about automation testing which uses selenium primarily . selenium/junit is test framework which does not work under src/main . Please let me know if there is any other approach to resolve this conflict.

正如我上面提到的,我在src / main文件夹中没有MainTest.java,因为我们的项目只是关于主要使用selenium的自动化测试。 selenium / junit是测试框架,在src / main下不起作用。如果有任何其他方法可以解决这个冲突,请告诉我。

1 个解决方案

#1


3  

Place all the files that are supposed to be part of the packaged jar into /src/main, not /src/test.

将应该是打包jar的一部分的所有文件放入/ src / main,而不是/ src / test。

/src/test should contain your project's test files (unit tests etc) and are not part of the final artifact. That's why java cannot find the class in the jar.

/ src / test应该包含项目的测试文件(单元测​​试等),而不是最终工件的一部分。这就是java无法在jar中找到类的原因。

#1


3  

Place all the files that are supposed to be part of the packaged jar into /src/main, not /src/test.

将应该是打包jar的一部分的所有文件放入/ src / main,而不是/ src / test。

/src/test should contain your project's test files (unit tests etc) and are not part of the final artifact. That's why java cannot find the class in the jar.

/ src / test应该包含项目的测试文件(单元测​​试等),而不是最终工件的一部分。这就是java无法在jar中找到类的原因。