Running mvn shade on a pre-existing uber jar - maven

I have a uber jar that's already generated (not via Maven; I just have the jar). I need to use it along with another jar, which has dependency conflicts. I'd like to shade all the libs in the first uber jar, except for a few classes which need to be public.
What is the setup to take an existing jar, and use Maven shade plugin to rename most (except for a whitelist) of its classes, generating a new uber jar?

You can do a "rename-all" type of activity with maven-shade-plugin. For example:
<relocations>
<relocation>
<pattern></pattern>
<shadedPattern>relocated.</shadedPattern>
<excludes>
<exclude>com.myfirm.level1.level2.*</exclude>
</excludes>
</relocation>
</relocations>
Will essentially move every package a.b.c to relocated.a.b.c (except for the explicit pattern I excluded).
It appears that the relocation process moves all references to classes that match that pattern, regardless if that class is defined within a dependency used to construct the jar.
The following stack trace (obtained from trying to run a main() defined in a jar that was constructed using the relocation above) shows that a reference to java.lang.Object was renamed to relocated.java.lang.Object, and that (obviously) the relocated. version can't be found.
java.lang.NoClassDefFoundError: relocated/java/lang/Object
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: relocated.java.lang.Object
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
Exception in thread "main"

Related

Spring Boot and Vaadin - ClassNotFoundException: org.springframework.web.context.support.WebApplicationObjectSupport

I have generated Spring Boot Vaadin project at start.spring.io with just the Vaadin dependency (Maven project, Spring Boot 1.5.1, Vaadin dependency).
I have unzipped the dowloaded .zip file and imported the project to STS (File -> Import -> Existing Maven Projects). I tried to run the application but I got this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/web/context/support/WebApplicationObjectSupport
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:570)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:697)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:640)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:515)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:508)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1189)
at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:862)
at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:848)
at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:834)
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:788)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:325)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.example.MyvaadinApplication.main(MyvaadinApplication.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.support.WebApplicationObjectSupport
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 60 more
The project can be build by Maven (clean install) when skipping tests. With tests enabled it fails with the same error.
I have already tried:
to import project and run it in different IDEs (STS, Eclipse, Idea)
run the application as Spring Boot App, Java Application, from test class
other versions of Spring Boot (1.5.1, 1.4.4)
different Java 8 (jdk1.8.0_65, jdk1.8.0_121)
restart IDE
Based on the comment from p.streef I found the solution. I cleared Maven repository (more precisely I deleted content of .m2\repository\org\springframework folder where Spring .jar files are located) and run the application again. Then it worked.
Edit
It seems like frequent problem. Recently I tried to create a new Spring Boot project with Vaadin, JPA and H2 dependencies. At first I got:
spring boot Error creating bean with name 'entityManagerFactory'
I deleted jars in .m2\repository\org\hibernate and run mvn clean install and got:
spring boot NoClassDefFoundError: javassist/bytecode/ClassFile
I deleted jars in .m2\repository\org\javassist and run mvn clean install
Now it worked. So when you get some error running newly created Spring Boot project saying that some class is missing, clean jars in your Maven repository and try it again.

Gradle not satisfying dependencies?

I'm trying to build this project from github:
https://github.com/tomakehurst/wiremock
The thing is, when I download it and run gradle build, I don't think all the required jar files are being loaded onto the class path. I get this error when I actually try to run the thing:
Exception in thread "main" java.lang.NoClassDefFoundError: joptsimple/OptionParser
at com.github.tomakehurst.wiremock.standalone.CommandLineOptions.<init>(CommandLineOptions.java:75)
at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:49)
at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:110)
Caused by: java.lang.ClassNotFoundException: joptsimple.OptionParser
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)
... 3 more
What gives? Isn't grade supposed to have downloaded this dependency by itself?
Try to execute gradle clean build jarAll
See this github issue for reference
From what I see in the build code, the task jarAll handles the joptsimple classes (and various others)

Trouble setting Classpath for Cpsuite with Maven

Using Maven I am creating a jar-with-dependencies for a project that uses ClasspathSuite to create a JUnit test suite. When running the jar from the command line I am running into an issue where the project classpath isn't being supplied like it would be in Eclipse.
Exception in thread "main" java.lang.NoClassDefFoundError: com/<dir>/BaseCategoryRunner
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
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: com.<dir>.BaseCat
egoryRunner
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
I need the project classpath so that ClasspathSuite can scan my files to build a junit class suite. I have been searching a number of ways to allow maven to include the classpath in my pom such as <additionalClasspathElements> or <Class-Path> but I cannot get the value to appear in the system property java.class.path. At this point I am unsure if I am just walking down a rabbit hole that isn't going to work.
Is it possible to have a classpath in my jar's manifest that will point to one of the jar's directory? In this case ./com/....
How can I have this classpath referenced so that maven will include it when the jar is executed?

Failure running JavaFX 2.2 using Maven

I use Zen's Maven plugin to develop my JavaFX 2.2 application using Maven. The following is the plug-in declaration.
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
I fixed the classpath using the following command:
mvn com.zenjava:javafx-maven-plugin:2.0:fix-classpath
and the fixing probably succeeded.
The compilation also worked but the process did not generate JavaFX main files such as the following:
/com/javafx/main/Main$1.class
/com/javafx/main/Main$2.class
/com/javafx/main/Main.class
/com/javafx/main/NoJavaFXFallback.class
Running the project produces the following exceptions:
java.lang.NoClassDefFoundError: javafx/application/Application
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
What is going wrong and how can I fix it?
The main jar doesn't contain these classes, but the plugin generates an additional jar in the "target/jfx" directory (or something like this). Use that jar to start your application.

Issue creating concordion-extensions-demo with Maven

I am trying to create the concordion-extensions-demo (located at http://concordion.org/Download.html) project with Maven.
When I run the command mvn test I receive errors ...
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
In the POM there are variables for ..
concordion.version, concordion-extensions.version and slf4j.version.
Do I need to update these tags with actual version numbers?
As is probably obvious I am not an experienced software devoloper or have much experience with Maven. Does anyone have an idea as to what the problem could be and how I could resolve this?
Thanks.
Tam.

Resources