加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – SpringBoot loader.path无法加载外部Jar

发布时间:2020-12-15 04:34:02 所属栏目:Java 来源:网络整理
导读:我正在使用 springboot作为webapp,我正在尝试设置一个外部目录,该目录将包含最终用户可能选择使用的各种JDBC驱动程序.要做到这一点,我补充说: loader.path=/opt/myapp/lib/ 到我的application.properties文件,这是由PropertySourcesPropertyResolver选取的
我正在使用 springboot作为webapp,我正在尝试设置一个外部目录,该目录将包含最终用户可能选择使用的各种JDBC驱动程序.要做到这一点,我补充说:

loader.path=/opt/myapp/lib/

到我的application.properties文件,这是由PropertySourcesPropertyResolver选取的

2016-04-28 17:27:38.739 DEBUG 22539 --- [restartedMain] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'loader.path' in [applicationConfigurationProperties] with type [String] and value '/opt/myapp/lib/'

我的问题是我似乎无法从任何放入此目录的jar中加载任何JDBC驱动程序,我缺少什么?我正在使用默认的嵌入式tomcat服务器.当我尝试使用Class.forName加载驱动程序时,我得到以下内容,就像该目录中不存在jar.

public Connection buildConnection(DataSource dataSource) throws ClassNotFoundException,SQLException {

    if (dataSource == null) {
        throw new NullPointerException("Data Source is null!");
    }

    if (!dataSource.isReady()) {
        throw new IllegalArgumentException("Data Source is reporting that it is not ready!");
    }

    logger.debug("Loading JDBC Driver: {}",dataSource.getDriverClass());
    Class.forName(dataSource.getDriverClass());
    logger.debug("Loaded Driver: {}",dataSource.getDriverClass());

    logger.debug("Attempting to build connection using: {}",dataSource.getConnectionString());

    DriverManager.setLoginTimeout(10);
    Connection c = DriverManager.getConnection(dataSource.getConnectionString(),dataSource.getUserName(),dataSource.getPassword());
    if (c != null) {
        c.setAutoCommit(true);
        c.setReadOnly(true);
        return c;
    }
    throw new NullPointerException("Unable to create connection!");
}

这是抛出的异常

2016-04-28 17:38:53.525 DEBUG 22539 --- [nio-8081-exec-5] c.c.reportout.processor.JobProcessor     : Loading JDBC Driver: com.mysql.jdbc.Driver
2016-04-28 17:38:53.526  WARN 22539 --- [nio-8081-exec-5] c.c.reportout.processor.JobProcessor     : Unable to successfully test connection: com.mysql.jdbc.Driver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) ~[spring-boot-devtools-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_91]
at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_91]

关于如何调试这个或我做错了的任何指针?

谢谢

解决方法

所以经过大量的谷歌搜索后,事实证明我能够通过添加以下内容来解决它:

<configuration>
    <layout>ZIP</layout>
</configuration>

到我的pom文件中的spring-boot-maven-plugin插件.所以工作版现在看起来像这样:

<plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读