0

当我从 NetBeans 启动我的应用程序时,它工作正常。但是当我从 jar 执行我的应用程序时,我收到了这个错误:

无法加载 spring 应用程序上下文:java.util.concurrent.CompletionException:java.lang.IllegalArgumentException:在 META-INF/spring.factories 中找不到自动配置类。如果您使用的是自定义包装,请确保该文件是正确的。

这是我的POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>infn.lns</groupId>
<artifactId>ConsoleLNSMaven</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>ConsoleLNSMaven</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/>
    <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mainClass>lns.MainApp</mainClass>

    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

    <opencv.directory>/home/gianfranco/opencv/build/lib</opencv.directory>
    <!--<opencv.directory>/home/dmnrserver/opencv/build/lib</opencv.directory>-->
</properties>

<organization>
    <name>INFN LNS</name>
</organization>

<dependencies>

     <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
     </dependency>

     <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
     </dependency>

     <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>11.0.2</version>
     </dependency>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>11.0.2</version>
     </dependency>



  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
</exclusions>
  </dependency>
  <dependency>
      <groupId>de.roskenet</groupId>
      <artifactId>springboot-javafx-support</artifactId>
      <version>2.1.6</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>org.javassist</groupId>
        </exclusion>

        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>

    </exclusions>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
  </dependency>
  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
  </dependency>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
  </dependency>
  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
  </dependency>
  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>

  </dependency>
  <dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>opencv</artifactId>
    <version>3.4.1-1.4.1</version>
  </dependency>
  <dependency>
    <groupId>org.controlsfx</groupId>
    <artifactId>controlsfx</artifactId>
    <version>8.40.14</version>
  </dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.22.0-GA</version>
    <scope>runtime</scope>
</dependency>


</dependencies>

<build>
     <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addClasspath>true</addClasspath>
                <mainClass>lns.MainApp</mainClass>
            </configuration>
            <executions>
                  <execution>
                      <goals>
                           <goal>repackage</goal>
                      </goals>
                  </execution>
            </executions>
        </plugin>
    </plugins>
</build>

当我使用此命令运行程序时,它可以工作:

mvn "-Dexec.args=-Djava.library.path=/home/gianfranco/opencv/build/lib --module-path /home/gianfranco/Scaricati/javafx-sdk-11.0.2/lib --add-modules javafx.controls --add-exports=javafx.graphics/com.sun.javafx.util=ALL-UNNAMED --add-exports=javafx.base/com.sun.javafx.reflect=ALL-UNNAMED --add-exports =javafx.base/com.sun.javafx.beans=ALL-UNNAMED --add-exports=javafx.graphics/com.sun.glass.utils=ALL-UNNAMED --add-exports=javafx.graphics/com.sun .javafx.tk=ALL-UNNAMED -classpath %classpath lns.MainApp" -Dexec.executable=/usr/lib/jvm/java-11-oracle/bin/java process-classes org.codehaus.mojo:exec-maven-插件:1.6.0:执行

但是当我使用这个时它不起作用,它给了我这个错误:

java --module-path /home/gianfranco/Scaricati/javafx-sdk-11.0.2/lib --add-modules=javafx.controls,javafx.fxml,javafx.media,javafx.swing -jar target/ConsoleLNSMaven-1.0 。罐

4

1 回答 1

0

这是解决方案:

  1. 仅在 POM.xml 中添加插件

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>
        <mainClass>${start-class}</mainClass>
    </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
    </executions>
    </plugin>
    </plugins>
    
  2. pom.xml 中的一个属性

    <properties>
      <start-class>com.xxx.yyy.MainClass</start-class>
    </properties>
    
  3. 运行命令

     mvn clean package spring-boot:repackage
    
  4. 现在运行在目标文件夹中创建的 jar(它现在包括所有依赖项)

     java -jar my-service-0.0.1-SNAPSHOT.jar
    
于 2020-04-07T20:11:22.477 回答