Reputation: 3868
I have always created uber jar with spring-boot and used java -jar springboot.jar
command to start container. Now my requirement is changed and I need to point to external classpath as follows which is not fixed. I still create uber jar for spring boot.
java -Dprofile=dev -cp springboot.jar:/usr/local/hadoop/lib/*:/usr/local/hbase/lib/* com.myapp.Application
It throws following:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/scheduling/annotation/AsyncConfigurer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: org.springframework.scheduling.annotation.AsyncConfigurer
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
My Application class uses org.springframework.scheduling.annotation.AsyncConfigurer
Here's maven:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeGroupIds>org.apache.hadoop,org.apache.hbase</excludeGroupIds>
<excludeArtifactIds>hadoop-yarn-common,hadoop-yarn-client,hadoop-annotations,hadoop-yarn-api,hadoop-mapreduce-client-jobclient,
hadoop-mapreduce-client-shuffle,hadoop-mapreduce-client-app,hadoop-mapreduce-client-core,hadoop-mapreduce-client-common,
hadoop-yarn-server-common,hadoop-hdfs,hadoop-auth,hadoop-common,parquet-hadoop,hadoop-client</excludeArtifactIds>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.myApp.Application</mainClass>
</manifest>
<manifestEntries>
<Build-Scm-Branch>${scmBranch}</Build-Scm-Branch>
<Build-Revision-Number>${buildNumber}</Build-Revision-Number>
<Timestamp>${timestamp}</Timestamp>
</manifestEntries>
</archive>
</configuration>
</plugin>
One thing I notice that spring-boot-maven-plugin keeps all the uber jars inside lib folder instead of extracting every class in put it in flat hierarchy like shade plugin. I don't know if that's the reason -cp is not recognizing any uber classes ?
Thanks
Upvotes: 1
Views: 2074
Reputation: 101
If you use the PropertiesLauncher by setting the <layout>ZIP</laout>
for the spring-boot-maven-plugin, you can extend your classpath using the loader.path property. E.g.:
java -Dprofile=dev -Dloader.path=springboot.jar,/usr/local/hadoop/lib,/usr/local/hbase/lib -jar springboot.jar
See http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#executable-jar-property-launcher-features in spring boots reference guide.
Upvotes: 1