I am using springboot for a webapp and I am attempting to setup an external directory that will hold various JDBC drivers that an end user may select to use. To do this I am adding:
loader.path=/opt/myapp/lib/
to my application.properties file, and this is picked up by the 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/'
My problem is I can't seem to load any JDBC drivers from any jars that I drop into this directory, what am I missing? I am using the default embedded tomcat server. When I attempt to load the driver using Class.forName, I get the following, like the jars aren't present in that directory.
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!");
}
This is the exception that is thrown
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]
Any pointers as to how I can debug this or what I am doing wrong?
Thanks
There are two simple ways to do that: the simplest one is to add the property on the command line. Also, you will not launch the Spring Boot application in the usual format (java -jar application). On the other hand, you will launch the PropertiesLauncher class but adding in the classpath your Spring Boot application.
The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.
loader. path can contain directories (which are scanned recursively for jar and zip files), archive paths, a directory within an archive that is scanned for jar files (for example, dependencies. jar!/lib ), or wildcard patterns (for the default JVM behavior). Archive paths can be relative to loader.
Spring Boot’s “JarFile” Class The core class used to support loading nested jars is org.springframework.boot.loader.jar.JarFile . It lets you load jar content from a standard jar file or from nested child jar data.
The spring-boot-loader modules lets Spring Boot support executable jar and war files. If you use the Maven plugin or the Gradle plugin, executable jars are automatically generated, and you generally do not need to know the details of how they work.
Load Using Profiles Additional to the default one, Spring Boot provides the way to use active profiles to load additional configurations via java command or java programmatically. Now, Add the new property file named application-error.properties in the classpath and add error codes into the file.
Loading the properties file from an external location can be done in multiple ways but you should decide which one is suitable for your scenario. 2. Loading from application.properties By default, Spring boot will load all the properties from the application.properties file which is located at " src/main/resources ".
So after much googling, it turns out I was able to resolve it by adding:
<configuration>
<layout>ZIP</layout>
</configuration>
to the spring-boot-maven-plugin plugin in my pom file. So the working version looks like this now:
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With