Spring启动-Java FAT Jar-NoClassFoundError

Spring启动-Java FAT Jar-NoClassFoundError,java,spring,spring-boot,maven,noclassdeffounderror,Java,Spring,Spring Boot,Maven,Noclassdeffounderror,在我的Java项目中,我很难访问我的一个API。此API是通过外部JAR访问的。通过定期运行SpringBoot,我能够访问API并获取一些数据,但是当我从JAR运行时,我会得到NoClassFoundError。为了解决这个问题,我使用Maven的shade插件创建了一个胖jar,它将捆绑我所有的外部依赖,它已经完成了,但是从shade jar运行程序时,我遇到了同样的问题 下面是我的pom.xml <?xml version="1.0" encoding="

在我的Java项目中,我很难访问我的一个API。此API是通过外部JAR访问的。通过定期运行SpringBoot,我能够访问API并获取一些数据,但是当我从JAR运行时,我会得到NoClassFoundError。为了解决这个问题,我使用Maven的shade插件创建了一个胖jar,它将捆绑我所有的外部依赖,它已经完成了,但是从shade jar运行程序时,我遇到了同样的问题

下面是我的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.edm</groupId>
    <artifactId>EDMProd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>EDMProd</name>
    <description>EDM API Service</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- Mentor Dependencies -->
        
        <dependency>
            <groupId>com.mentor.datafusion.oi</groupId>
            <artifactId>com.mentor.datafusion.oi</artifactId>
            <version>1.0.0</version>
        </dependency>     
        
        <dependency>
            <groupId>dfo</groupId>
            <artifactId>dfo</artifactId>
            <version>1.0.0</version>
        </dependency>        

        <dependency>
            <groupId>dfutils</groupId>
            <artifactId>dfutils</artifactId>
            <version>1.0.0</version>
        </dependency>    

        <dependency>
            <groupId>com.mentor.datafusion.dfo.is3</groupId>
            <artifactId>com.mentor.datafusion.dfo.is3</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.mentor.is3.edm.login.api</groupId>
            <artifactId>com.mentor.is3.edm.login.api</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>apache-logging-log4j</groupId>
            <artifactId>apache-logging-log4j</artifactId>
            <version>1.0.0</version>
        </dependency>       
        
        <dependency>
            <groupId>com.mentor.datafusion.dfo.is3.api</groupId>
            <artifactId>com.mentor.datafusion.dfo.is3.api</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>com.mentor.datafusion.dfo.is3.common</groupId>
            <artifactId>com.mentor.datafusion.dfo.is3.common</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>DFTunnelClient</groupId>
            <artifactId>DFTunnelClient</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>is3-server-api</groupId>
            <artifactId>is3-server-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>com.mentor.is3.server.dms.api</groupId>
            <artifactId>com.mentor.is3.server.dms.api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
    <finalName>EDM Library Web Service</finalName>
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
              <execution>
              <id>shade-my-jar</id>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>com.edm.EdmProdApplication</mainClass>
                    </transformer>
                  </transformers>
                </configuration>
              </execution>
            </executions>
          </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这个问题我已经解决太久了,我只是不知道该怎么办。我觉得我已经尝试了我在堆栈溢出上看到的一切

下面是我尝试连接到API时运行的源代码

package com.edm;

import com.mentor.datafusion.oi.OIObjectManager;
import com.mentor.datafusion.oi.OIObjectManagerFactory;
import com.mentor.datafusion.oi.login.OIAuthenticate;
import com.mentor.datafusion.oi.login.OIAuthenticateFactory;
import com.mentor.datafusion.oi.login.OILoginData;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class Connect {

    static OIObjectManagerFactory omf;
    static OIObjectManager om;
    static String SERVER_URL;
    static String SERVER_USERNAME;
    static String SERVER_PASSWORD;
    static String PRODUCTION_LIBRARY;
    static String OSAT_LIBRARY;
    static String CAPACITOR_CLASS;
    static String RESISTOR_CLASS;
    static String INDUCTOR_CLASS;

    public Connect() throws IOException {
        // Read Configuration File
        String currentDirectory = System.getProperty("user.dir");
        Path configurationFile_path= Paths.get(currentDirectory).getParent();
        FileInputStream inputStream = new FileInputStream(configurationFile_path + "/src/main/resources/configuration.properties");
        Properties prop = new Properties();
        prop.load(inputStream);

        // Initialize Server Variables
        this.CAPACITOR_CLASS = prop.getProperty("CAPACITOR_CLASS");
        this.RESISTOR_CLASS = prop.getProperty("RESISTOR_CLASS");
        this.INDUCTOR_CLASS = prop.getProperty("INDUCTOR_CLASS");
        this.PRODUCTION_LIBRARY = prop.getProperty("PRODUCTION_LIBRARY");
        this.SERVER_URL = prop.getProperty("SERVER_URL");
        this.OSAT_LIBRARY = prop.getProperty("OSAT_LIBRARY");
        this.SERVER_USERNAME = prop.getProperty("SERVER_USERNAME");
        this.SERVER_PASSWORD = prop.getProperty("SERVER_PASSWORD");
    }

    public static void ConnectToEDM(String libraryName) {

        try {
            OILoginData loginData = OIAuthenticateFactory.createLoginData("myLoginConfig");
            loginData.setServer(SERVER_URL);
            loginData.setUsername(SERVER_USERNAME);
            loginData.setPassword(SERVER_PASSWORD);
            loginData.setProdLib(PRODUCTION_LIBRARY);
            String applicationName = "Component Library";

            OIAuthenticate auth = OIAuthenticateFactory.createBatchAuthenticate(loginData);
            omf = auth.login(applicationName);
            
            System.out.println("Connected to library: " + libraryName);
        }
        catch (Exception e) {
            System.out.println("Connection failed");
            e.printStackTrace();
        }


        try {
            om = omf.createObjectManager();
            om.setLibraryConfiguration(libraryName);
            System.out.println("Successfully connected to " + libraryName + " EDM Library.");
        }
        catch (Exception e) {
            System.out.println("Error: " + e.toString());
            e.printStackTrace();
        }
    }
}

我相信真正的问题是,我只需要4个外部依赖项来运行我的API。在本地可以,但当我打包到JAR中时,我相信这些依赖依赖依赖于其他依赖,所以当在本地运行时,我相信我的系统知道从哪里获取这些依赖,但是当打包到JAR中时,找不到这些依赖

作为旁注:我将依赖项导入项目的方式是

  • 在类路径中添加外部jar(Eclipse)
  • 将我导入的jar作为依赖项包含在我的pom.xml中

  • 我是新来的,所以我不确定这是否是正确的方法。

    看起来您试图从需要身份验证的特定回购中访问工件

    检查.m2文件夹中的settings.xml,使用您试图连接的存储库的配置文件


    让我知道你的想法

    我在我的.m2文件夹中没有看到settings.xml文件。你应该使用spring引导maven插件,而不是maven shade插件…有什么原因吗?
    package com.edm;
    
    import com.mentor.datafusion.oi.OIObjectManager;
    import com.mentor.datafusion.oi.OIObjectManagerFactory;
    import com.mentor.datafusion.oi.login.OIAuthenticate;
    import com.mentor.datafusion.oi.login.OIAuthenticateFactory;
    import com.mentor.datafusion.oi.login.OILoginData;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Properties;
    
    public class Connect {
    
        static OIObjectManagerFactory omf;
        static OIObjectManager om;
        static String SERVER_URL;
        static String SERVER_USERNAME;
        static String SERVER_PASSWORD;
        static String PRODUCTION_LIBRARY;
        static String OSAT_LIBRARY;
        static String CAPACITOR_CLASS;
        static String RESISTOR_CLASS;
        static String INDUCTOR_CLASS;
    
        public Connect() throws IOException {
            // Read Configuration File
            String currentDirectory = System.getProperty("user.dir");
            Path configurationFile_path= Paths.get(currentDirectory).getParent();
            FileInputStream inputStream = new FileInputStream(configurationFile_path + "/src/main/resources/configuration.properties");
            Properties prop = new Properties();
            prop.load(inputStream);
    
            // Initialize Server Variables
            this.CAPACITOR_CLASS = prop.getProperty("CAPACITOR_CLASS");
            this.RESISTOR_CLASS = prop.getProperty("RESISTOR_CLASS");
            this.INDUCTOR_CLASS = prop.getProperty("INDUCTOR_CLASS");
            this.PRODUCTION_LIBRARY = prop.getProperty("PRODUCTION_LIBRARY");
            this.SERVER_URL = prop.getProperty("SERVER_URL");
            this.OSAT_LIBRARY = prop.getProperty("OSAT_LIBRARY");
            this.SERVER_USERNAME = prop.getProperty("SERVER_USERNAME");
            this.SERVER_PASSWORD = prop.getProperty("SERVER_PASSWORD");
        }
    
        public static void ConnectToEDM(String libraryName) {
    
            try {
                OILoginData loginData = OIAuthenticateFactory.createLoginData("myLoginConfig");
                loginData.setServer(SERVER_URL);
                loginData.setUsername(SERVER_USERNAME);
                loginData.setPassword(SERVER_PASSWORD);
                loginData.setProdLib(PRODUCTION_LIBRARY);
                String applicationName = "Component Library";
    
                OIAuthenticate auth = OIAuthenticateFactory.createBatchAuthenticate(loginData);
                omf = auth.login(applicationName);
                
                System.out.println("Connected to library: " + libraryName);
            }
            catch (Exception e) {
                System.out.println("Connection failed");
                e.printStackTrace();
            }
    
    
            try {
                om = omf.createObjectManager();
                om.setLibraryConfiguration(libraryName);
                System.out.println("Successfully connected to " + libraryName + " EDM Library.");
            }
            catch (Exception e) {
                System.out.println("Error: " + e.toString());
                e.printStackTrace();
            }
        }
    }