How to make fat-jar with Embedded EJB container - java

I'm trying to create a fat-jar using Embedded EJB container.
My problem is that when I try to run the jar file, IllegalArgumentException occurred.
Please help me.
My Code.
public static void main(String[] args) throws NamingException {
try (EJBContainer container = EJBContainer.createEJBContainer()) {
String jndi = "java:global/javaee-batch/MainLogic";
MainLogic logic = (MainLogic) container.getContext().lookup(jndi);
logic.run();
}
}
If I can execute by maven, this code is successful.
here are my poms:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>simplebatch</groupId>
<artifactId>JavaEE-Batch</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>simplebatch.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-sample-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
</dependencies>
</project>
When I execute "JavaEE-Batch-1.0-SNAPSHOT-jar-with-dependencies.jar", this error occurred.
Sep 15, 2014 8:21:46 PM org.glassfish.api.ActionReport failure
SEVERE: Exception while deploying the app [javaee-batch]
Sep 15, 2014 8:21:46 PM com.sun.enterprise.v3.server.ApplicationLifecycle deploy
SEVERE: Exception during lifecycle processing
java.lang.IllegalArgumentException: Referencing error: this bundle has no bean of name: MEJBBean
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.getEjbByName(EjbBundleDescriptorImpl.java:323)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.getEjbByName(EjbBundleDescriptorImpl.java:307)
at org.glassfish.ejb.deployment.node.runtime.EjbNode.setElementValue(EjbNode.java:133)
at com.sun.enterprise.deployment.node.SaxParserHandler.endElement(SaxParserHandler.java:617)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLNSDTDValidator.endNamespaceScope(XMLNSDTDValidator.java:266)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2005)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:879)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
What do I do to

Is MEJBBean part of your beans or an external library?
The stacktrace is missing more information. There should be at least caused by... There is a likelihood that maven (on whichever ide you use) sets the correct dependency paths, but on building with dependency, this path is not copied, either because this external dependency has scope provided or is set as optional

Related

Can run in Intellij, but jar file fails

Instead of running Quarkus, I'm running OptaPlanner through a Main class entry point in Kotlin.
This works great within Intellij where I have a simple Run Configuration set up for it.
object Main {
lateinit var solverFactory: SolverFactory<VehicleRoutingSolution>
lateinit var solver: Solver<VehicleRoutingSolution>
lateinit var scoreManager: ScoreManager<VehicleRoutingSolution, SimpleLongScore>
#JvmStatic
fun main(args: Array<String>) {
...
However, when I create an Intellij artifact for a jar and try to run it, I get this:
java -jar acme.jar
Apr. 28, 2022 5:21:13 P.M. com.sun.xml.bind.v2.runtime.reflect.opt.Injector <clinit>
SEVERE: null
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:573)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:166)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:51)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:157)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:255)
at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:99)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:150)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:484)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:301)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:88)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:80)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.<init>(SolverConfigIO.java:27)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:213)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:188)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:128)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:103)
at org.optaplanner.core.api.solver.SolverFactory.createFromXmlResource(SolverFactory.java:55)
at org.acme.bootstrap.Main.initOptaPlanner(Main.kt:112)
at org.acme.bootstrap.Main.main(Main.kt:43)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
at java.base/java.lang.Class.getMethod(Class.java:2227)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:170)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:166)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
... 31 more
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:294)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:66)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:57)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:157)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:255)
at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:99)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:150)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:484)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:301)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:88)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:80)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.<init>(SolverConfigIO.java:27)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:213)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:188)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:128)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:103)
at org.optaplanner.core.api.solver.SolverFactory.createFromXmlResource(SolverFactory.java:55)
at org.acme.bootstrap.Main.initOptaPlanner(Main.kt:112)
at org.acme.bootstrap.Main.main(Main.kt:43)
Shutting down
In solverConfig.xml I'm using <domainAccessType>REFLECTION</domainAccessType> because I cannot get Gizmo to work. I'm not sure if that's related or not, but why is there a difference between an Intellij Run Config and the Jar artifact?
Thanks
*** UPDATE ***
I still get the above error message - even when creating a very basic OptaPlanner project.
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>org.acme</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.6.21</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<optaplanner.version>8.20.0.Final</optaplanner.version>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<finalName>foo</finalName>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.acme.bootstrap.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-bom</artifactId>
<type>pom</type>
<version>${optaplanner.version}</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-core</artifactId>
</dependency>
</dependencies>
</project>
Here's what I run with Maven:
mvn clean compile package
java -jar ./target/foo-jar-with-dependencies.jar
Note: I'm using openjdk-17.0.2
java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
It still runs fine within IntelliJ's system, but from Maven it just throws the above error.
Also, here's my solverConfig.xml for reference.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<solver xmlns="https://www.optaplanner.org/xsd/solver">
<solutionClass>org.acme.domain.MySolution</solutionClass>
<entityClass>org.acme.domain.Visit</entityClass>
<scoreDirectorFactory>
<constraintProviderClass>org.acme.solver.MyConstraintProvider</constraintProviderClass>
</scoreDirectorFactory>
<termination>
<millisecondsSpentLimit>2000</millisecondsSpentLimit>
</termination>
</solver>
And my Main.kt file:
object Main {
lateinit var solverFactory: SolverFactory<MySolution>
lateinit var solver: Solver<MySolution>
lateinit var scoreManager: ScoreManager<MySolution, SimpleLongScore>
#JvmStatic
fun main(args: Array<String>) {
solverFactory = SolverFactory.createFromXmlResource("solverConfig.xml")
solver = solverFactory.buildSolver()
scoreManager = ScoreManager.create(solverFactory)
}
}
An answer to another question suggests you need an extra artifact in your project. Technically, optaplanner-core module brings it, but the scope is runtime - so that dependency may not show up in the JAR with dependencies. (?) You will need to include it yourself; you can confirm that by checking mvn dependency:tree in your project.
That said, I am not sure why we use the runtime scope here. Maybe we need to re-evaluate.
Also, seeing as you are using an uberjar, I'm going to point to another uberjar-related question to perhaps pre-empt more struggle.

How to add a spring-boot jar as a dependency in a new spring-boot project without changing the original pom

In order to add azure app insights in an old spring-boot jar(which already has micrometer), I've created a new spring-boot project and added it as a dependency. However, after a successful Maven build, while executing it as spring-boot app, I'm getting Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] exception. Here are my two POM files,
POM of the old spring-boot jar:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservice</artifactId>
<groupId>my-app-service</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-my-app</artifactId>
<dependencies>
<!-- few dependencies -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>app</classifier>
<mainClass>com.my.package.MyApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
POM of the new spring-boot project:
<?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>
<groupId>com.my.app.appinsights</groupId>
<artifactId>microservice-my-app-appinsights</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-my-app-appinsights</name>
<description>Demo project for Spring Boot</description>
<properties>
<applicationinsights.version>1.1.1</applicationinsights.version>
<java.version>1.8</java.version>
<applicationinsights.version>2.5.1</applicationinsights.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>my-app-service</groupId>
<artifactId>microservice-my-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>${applicationinsights.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webflux</artifactId>
<version>3.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>1.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>app</classifier>
<mainClass>com.my.app.appinsights.MicroserviceMyAppinsightsApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Exception stack:
2020-02-03 13:48:39.726 WARN 2420 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.microsoft.applicationinsights.autoconfigure.ApplicationInsightsWebMvcAutoConfiguration]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist
2020-02-03 13:48:39.734 INFO 2420 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-03 13:48:39.742 ERROR 2420 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.microsoft.applicationinsights.autoconfigure.ApplicationInsightsWebMvcAutoConfiguration]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:596)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:302)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:586)
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:108)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:805)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at com.my.app.appinsights.MicroserviceMyAppinsightsApplication.main(MicroserviceMyAppinsightsApplication.java:14)
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:682)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:995)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:332)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:586)
... 22 common frames omitted
It turns out that the azure app-insights needs spring-boot-starter-web to work properly. But since that jar is already present in the parent of the existing jar, I can not add it to my current POM file. This might mean I might not be importing the old jar in my new POM correctly.
How do I add the existing spring-boot jar as a dependency in my new spring-boot's POM file, without making any modifications in the existing jar or its POM?
Can you please check the packing of both the projects, if the child project has different packing then parent project, then you need to add component scan at your child main class from where you are running your project.
For example,
Parent project has package name
com.my.app
And
child project had package name
com.my.app.appinsights
then you need to add component scan in main class
#ComponentScan("com.my.app")
Spring boot by default scan beans from sub-packages of the main class. To override the component scan you need to add this annotation so that it will scan beans from given package also.

How can I configure the moditect-maven-plugin to use the auto-value library in my modular application?

I'm trying to build a modular application, and I'm attempting to use the moditect-maven-plugin in order to use the auto-value library (which is not modular).
Based on the instructions here, I came up with the following 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>moditect-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>${auto-value.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Beta2</version>
<executions>
<execution>
<id>generate-module-info</id>
<phase>generate-sources</phase>
<goals>
<goal>generate-module-info</goal>
</goals>
<configuration>
<modules>
<module>
<artifact>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</artifact>
<moduleInfo>
<name>com.google.auto.value.annotations</name>
<exports>
com.google.auto.value;
*;
</exports>
</moduleInfo>
</module>
</modules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<auto-value.version>1.7</auto-value.version>
</properties>
The important portion is the moditect-maven-plugin configuration. Since I need the com.google.auto.value.AutoValue annotation, I export that package and then give a relevant name to the module.
Just for reference, here's the class I'm using the annotation in:
package com.example.moditect.test;
import com.google.auto.value.AutoValue;
#AutoValue
abstract class Animal {
abstract String name();
abstract int numberOfLegs();
static Builder builder() {
return new AutoValue_Animal.Builder();
}
#AutoValue.Builder
abstract static class Builder {
abstract Builder setName(String value);
abstract Builder setNumberOfLegs(int value);
abstract Animal build();
}
}
...and here's the module-info.java file of my application:
module com.example.moditect.test {
requires com.google.auto.value.annotations;
}
However, when I run mvn clean install, I get the module not found error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project moditect-test: Compilation failure
[ERROR] /home/someuser/development/workspace/moditect-test/src/main/java/module-info.java:[2,35] module not found: com.google.auto.value.annotations
It does appear that the moditect-maven-plugin is working based on these logs that get outputted when I run mvn clean install:
[INFO] --- moditect-maven-plugin:1.0.0.Beta2:generate-module-info (generate-module-info) # moditect-test ---
writing to /home/someuser/development/workspace/moditect-test/target/moditect/auto.value.annotations/module-info.java
[INFO] Created module descriptor at /home/someuser/development/workspace/moditect-test/target/generated-sources/modules/com.google.auto.value.annotations/module-info.java
And for what it's worth, here's the generated module-info of this auto-value library (from the location that's logged above):
module com.google.auto.value.annotations {
exports com.google.auto.value;
exports com.google.auto.value.extension.memoized;
}
So I need some help figuring out what I did wrong. I'm assuming I just messed up the configuration of the moditect-maven-plugin, but I'm not sure how.
I made the silly mistake of using the goal generate-module-info instead of add-module-info. Just switching the goal with the correct one solved this issue.

Maven not finding apache RandomStringGenerator

I am trying to use the new Apache Commons Text new random string generator, but I can't find any usage on the Internet on how to properly import it. Maven builds successfully, but when I try to run my jar file from the command line with the following command
java -cp target/my-app-1.0.jar com.mycompany.app.App
…I get the below error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/text/RandomStringGenerator$Builder
at com.mycompany.app.App.main(App.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.RandomStringGenerator$Builder
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I also have this warning during the build:
The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more detail
Here is the Maven debug version:
[WARNING] The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model
[FATAL] Non-parseable POM C:\Users\me\.m2\repository\org\apache\commons\commons-text\1.1\commons-text-1.1.pom: only whitespace content allowed before start tag and not a (position: START_DOCUMENT seen a... #1:1) # line 1, column 1
Here is my App.java code:
package com.mycompany.app;
import org.apache.commons.text.RandomStringGenerator;
public class App {
public static void main(String[] args) {
// Generates a 20 code point string, using only the letters a-z
RandomStringGenerator generator = new RandomStringGenerator.Builder()
.withinRange('a', 'z').build();
String randomLetters = generator.generate(20);
System.out.println( randomLetters );
}
}
Here is my pom.xml:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
If you want to run you application from a jar, you will need an exploded jar. The default class loader will not find the apache jar from your jar.
You can use the shade plugin to create an exploded jar. Add something like below to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.mycompany.app.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, you can run with the following:
java -jar target/my-app-1.0.jar

Maven Embedded Jetty Container fails to load Taglibs: Unable to initialize TldLocationsCache

I'm using the Maven Cargo Plugin to startup a Jetty web container for running some integration tests in a separate project module.
The problem i'm battling with occurs when i added taglibs into the jsp pages and tried hitting them from the integration tests. When jetty tries to compile the pages it fails with this error:
org.apache.jasper.JasperException: Unable to initialize TldLocationsCache: null
The web app works fine when run in an installed Tomcat container or standalone Jetty run through maven on the command line, so i think the problem must be down to something to do with how cargo embeds jetty and then how jetty compiles the app.
I've tried running in forked and unforked modes, adding the taglibs to the container classpath, explicitly defining taglibs in web.xml and having no config there...all to no avail.
Here's the test project i'm using to debug:
web.xml
<?xml version='1.0' encoding='UTF-8'?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>taglibs-test</display-name>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
</jsp-config>
And the test module pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
<!-- Integration Test Embedded Servlet Container -->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<wait>false</wait>
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
<log>${project.build.directory}/log</log>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
</dependency>
</dependencies>
<systemProperties>
<DEBUG>true</DEBUG>
</systemProperties>
</container>
<configuration>
<properties>
<cargo.servlet.port>8090</cargo.servlet.port>
<cargo.logging>high</cargo.logging>
</properties>
<deployables>
<deployable>
<groupId>test</groupId>
<artifactId>web</artifactId>
<type>war</type>
<properties>
<context>taglibs-test</context>
</properties>
</deployable>
</deployables>
</configuration>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>test-compile</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>package</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here's the stacktrace from the error:
2009-01-24 13:53:06.766::WARN: /taglibs-test/index.jsp:
org.apache.jasper.JasperException: Unable to initialize TldLocationsCache: null
at org.apache.jasper.compiler.TldLocationsCache.init(TldLocationsCache.java:253)
at org.apache.jasper.compiler.TldLocationsCache.getLocation(TldLocationsCache.java:224)
at org.apache.jasper.JspCompilationContext.getTldLocation(JspCompilationContext.java:526)
at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:422)
at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1552)
at org.apache.jasper.compiler.Parser.parse(Parser.java:126)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:155)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
I've tracked this down to the following lines in jasper's TldLocationsCache:
private void init() throws JasperException {
if (initialized) return;
try {
processWebDotXml();
scanJars();
processTldsInFileSystem("/WEB-INF/");
initialized = true;
} catch (Exception ex) {
throw new JasperException(Localizer.getMessage(
"jsp.error.internal.tldinit", ex.getMessage()));
}
}
http://svn.apache.org/repos/asf/tomcat/jasper/tc6.0.x/src/share/org/apache/jasper/compiler/TldLocationsCache.java
Any help is greatly appreciated!!
cam
Bug report filed: http://jira.codehaus.org/browse/CARGO-651

Categories