Java build path errors on Eclipse - java

Recently, I've been playing a little bit with depency injections in Java. I'm a complete newbie in this field, and I don't really get, why in this simple example I keep receiving an error.
package michal.dependency;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Injector injector = Guice.createInjector(new ProjectModule());
Person person = injector.getInstance(Person.class);
person.greetFriend();
}
}
The error message I receive is as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList
at com.google.inject.internal.Errors.<clinit>(Errors.java:656)
at com.google.inject.internal.InternalInjectorCreator.<init>(InternalInjectorCreator.java:62)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:73)
at com.google.inject.Guice.createInjector(Guice.java:62)
at michal.dependency.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableList
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 6 more
I'm pretty sure by the way the necessary .jar file is included in the classpath.
Here comes the requested Person class, as requested:
package michal.dependency;
import com.google.inject.Inject;
public class Person {
private MessageService messageService;
#Inject
public Person (MessageService messageService)
{
this.messageService = messageService;
}
public void greetFriend ()
{
messageService.sendMessage("Hey!", "How are you?");
}
}
Thanks in advance.

I think you are missing Google collections, now known as Guava.
See Google Guice Wiki
JSR 330
Guice 4.0 requires JSR 330 on your classpath. This is the javax.inject.jar included in the guice download.
com.google.inject.internal
Many things inside com.google.inject.internal changed and/or moved. This is especially true for repackaged Guava (formerly Google Collections), cglib, and asm classes. All these classes are now hidden from IDE auto-import suggestions and are in new locations. You will have to update your code if you relied on any of these classes.

As other have suggested, it seems that something is missing from your classpath.
Maybe you could try using some sort of dependency management tool, for example Apache Maven?
It's a great tool for handling dependencies, used extensively in the java world. Depending on your IDE you will have lots of supportfor using it (my personal favourite is Intellij Idea with really great maven support, though Netbeans also does it pretty well).
I tried to prepare a maven pom.xml file it should look something like this. I tested the project with this and there are no compilation errors:
<?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>groupId</groupId>
<artifactId>test-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>

Related

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication.Tried all the solution but still not

Console Error
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at com.ayush.JavaApi.main(JavaApi.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
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
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ayush</groupId>
<artifactId>JavaCourse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
JavaClass with Main Method
package com.ayush;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class JavaApi {
public static void main(String[] args) {
SpringApplication.run(JavaApi.class,args);
}
}
I am running this program as java application but it is giving me error. i have try all the solution and also provided in the stackoverflow which is updating the maven but it is not working and still giving me the error.
thanks for the help in advance.
I've copied all your files to Intellij but it had no issue with your file.
Maybe hierarchical structure of your project (class JavaApi should be in JavaCourse package) or IDE setting (clean/rebuild, reimport pom) is wrong.
For beginning with Spring boot. You can generate project from https://start.spring.io/
I deleted the Project and created it again .It worked for me .Thanks everyone for the help

Spring Boot Error: java.lang.NoClassDefFoundError: org/springframework/util/Assert

I am new to Spring Boot and following some video tutorials to get started. I created a Maven project, added a parent dependency of artifact id spring-boot-starter-parent, added dependency for spring-boot-starter-web and spring-boot-starter-test. This is the Application.java:
#RestController
#EnableAutoConfiguration
public class Application {
#RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Note:
src/test/java and src/test/resources packages are empty. This is a Maven Project, I am using STS, performed "Maven -> Update Project..." and cleaned/build the entire project multiple times using STS IDE and I am getting the following error. Also, I tried to create a new project in STS and still I am getting the following error:
I am getting this error when I run this as a Spring Boot App:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/util/Assert
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:263)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
at com.boot.Application.main(Application.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.util.Assert
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)
... 5 more
Please help me in resolving this issue.
EDIT:
This 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.deloitte.boot</groupId>
<artifactId>boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-test</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
This is my project directory stricture:
I have fixed that issue like this, first I removed my folder .m2 and then I run mvn clean install into my directory spring boot project. that's all!
It is better to follow software principles while you are learning and making projects. Try to make project in a way that separation of concern is always achieved, that will make your code not just easy to understand, but to debug and fix too.
Change your application class like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Now make another package controller and make class in it for your testing
#RestController
//You can add this if you want or remove this class level mapping
#RequestMapping("/testApp")
public class TestController {
#RequestMapping("/")
public String home() {
return "Hello World";
}
}
For further help. please check this (official spring.io tutorial) simple example of Spring Boot App in STS.
And another one is this very simple and straightforward to get Spring Boot App up and running.
Edit: please add starter test in pom.xml as well.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>
Had the same issue with IntelliJ and a brand new project created from Spring Initializer. Saw this message when trying to compile manually:
[ERROR] error reading /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar; zip file is empty
Turns out the files downloaded from maven central were corrupt:
ls -al /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
-rw-r--r-- 1 mike staff 0 Jan 16 20:55
/Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
Removing the entire directory and rebuilding forced it to download a new copy of of the missing library:
./mvnw compile
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom (3.6 kB at 7.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar (1.3 MB at 3.6 MB/s)
Had the same issue, after I installed STS (before that, everything in Eclipse was going ok).
I removed the springframework folder that was inside the .m2 folder, then ran 'mvn clean install' in my project directory. After that I encountered an issue with my assertj-core-3.11.1.jar and mockito-core-2.23.0.jar (ZipFile invalid LOC header (bad signature)), so I also removed them and ran "mvn spring-boot:run".
Now it works (but I can't really explain why).

Spring Boot throws ClassNotFoundException with maven dependency for another project

I have Spring Boot project with simple EnvironmentPostProcessor implementation:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
public class DevProfilerResolverEnvironmentPostProcessor implements EnvironmentPostProcessor {
#Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
if (configurableEnvironment.getActiveProfiles().length == 0) {
if (System.getenv().get("OS").contains("Windows")) {
configurableEnvironment.addActiveProfile("DEV");
}
}
}
}
Also, I registered this class to sprig.factories:
org.springframework.boot.env.EnvironmentPostProcessor = com.example.demo.DevProfilerResolverEnvironmentPostProcessor
Now structure looks like:
Snippet from pom file:
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I executed with Maven:
mvn install
Now I want to use this EnvironmentPostProcessor implementation on another spring boot project. Thus I added it to dependency section for the new project:
<dependency>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
And I wrote simple service usage:
#Service
#Profile("DEV")
public class DeveloperService {
#Scheduled(cron = "1/1 * * * * ?")
public void doWork() {
System.out.println("Developers.... ");
}
}
and enabled scheduling for main class:
#SpringBootApplication
#EnableScheduling
public class LvivBootApplication {
public static void main(String[] args) {
SpringApplication.run(LvivBootApplication.class, args);
}
}
However, I got following exception after main execution:
14:56:09.822 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
java.lang.IllegalArgumentException: Unable to instantiate factory class: org.springframework.boot.env.EnvironmentPostProcessor
Caused by: java.lang.ClassNotFoundException: com.example.demo.DevProfilerResolverEnvironmentPostProcessor
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)
and dependency is added to the new project:
I am running it exactly on Windows environment.
The idea is when OS is Windows add DEV profile for the first project.
Second has service which prints to console dummy info when the profile is DEV and scheduled this printing for every second.
I can't find what is missed at this example?
SOLUTION:
For making from first project library jar pom should be fixed like following:
<!--<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>-->
Or simple to eliminate this plugin from pom file.
Your first jar is not a library jar. It's a repackaged boot jar, created by the spring boot plugin, containing the first spring boot application and all its dependencies, intended to be executed, and not to be used as a library.
If you want to use that as a library, you need to use the non-repackaged jar file, containing only the classes and resources of the project.
It's quite bizarre to have an application depend on another application, though. You should create a library project, only containing common common classes and resources, and use that as a dependency to your two spring boot applications.
I am posting because I had a similar error in similar circumstances - I searched for hours and the solution was very easy. I am using Eclipse for debugging - Eclipse doesn't use the produced jar from Maven build - it uses its own set of build paths with, as far as i understood, exploded classes etc.
My Maven project, the one that produced a JAR that I was including into my main project POM, didn't declare in any way some kind of dependency to the main project, e.g. via a common parent or whatever.
Eclipse seems to not understand that one of the dependencies I was using in the POM was a result of another local project - somehow, although the file (jar-with-dependencies) was in the Maven cache alright, it wasn't picking it up to copy to its own aforementioned set of classpath directories.
I needed to explicitly add it (my library project) into the main project via Project -> Properties -> Java Build Path -> Projects - adding it to the list titled "Required projects on the build path:"

CXF: java.lang.NoClassDefFoundError: javax/xml/ws/BindingProvider

I'm trying to implement Apache CXF in CQ. I've generated proxy classes from WSDL using cxf-codegen-plugin. Now my CxfServiceImpl.java looks like below -
import net.webservicex.ConvertTemperatureSoap;
import net.webservicex.TemperatureUnit;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
#Component(label = "CXF Service", immediate = true, metatype = true)
#Service(CxfService.class)
public class CxfServiceImpl implements CxfService {
private ConvertTemperatureSoap convertTemperatureSoap;
#Override
public double convertCelsiusToFahrenheit(double valueToConvert) {
return convertTemperatureSoap.convertTemp(
valueToConvert,
TemperatureUnit.DEGREE_CELSIUS,
TemperatureUnit.DEGREE_FAHRENHEIT);
}
#Activate
protected final void activate(final ComponentContext context) {
convertTemperatureSoap =
JaxWsClientFactory.create(
ConvertTemperatureSoap.class,
"http://www.w3schools.com/webservices/tempconvert.asmx");
}
}
JaxWsClientFactory.java looks like -
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.BusFactory;
public class JaxWsClientFactory {
public static <T> T create(Class<T> clazz, String portUrl) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(BusFactory.class.getClassLoader());
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(clazz);
factory.setAddress(portUrl);
return (T) factory.create();
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
}
I am able to create OSGI bundle and uploaded in Felix console. But when I try to activate bundle then I get error as below and bundle status is showing as Active.
24.02.2014 20:38:41.104 *ERROR* [127.0.0.1 [1393254521079] POST /system/console/bundles/300 HTTP/1.1] com.adobe.cq.customer-bundle [com.adobe.cq.CxfServiceImpl] The activate method has thrown an exception (java.lang.NoClassDefFoundError: javax/xml/ws/BindingProvider) java.lang.NoClassDefFoundError: javax/xml/ws/BindingProvider
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2167)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1471)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1882)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.adobe.cq.JaxWsClientFactory.create(JaxWsClientFactory.java:12)
at com.adobe.cq.CxfServiceImpl.activate(CxfServiceImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.scr.impl.helper.BaseMethod.invokeMethod(BaseMethod.java:236)
at org.apache.felix.scr.impl.helper.BaseMethod.access$500(BaseMethod.java:37)
at org.apache.felix.scr.impl.helper.BaseMethod$Resolved.invoke(BaseMethod.java:613)
at org.apache.felix.scr.impl.helper.BaseMethod.invoke(BaseMethod.java:496)
at org.apache.felix.scr.impl.helper.ActivateMethod.invoke(ActivateMethod.java:149)
at org.apache.felix.scr.impl.manager.ImmediateComponentManager.createImplementationObject(ImmediateComponentManager.java:251)
at org.apache.felix.scr.impl.manager.ImmediateComponentManager.createComponent(ImmediateComponentManager.java:119)
at org.apache.felix.scr.impl.manager.AbstractComponentManager$Unsatisfied.activate(AbstractComponentManager.java:1518)
at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:550)
at org.apache.felix.scr.impl.manager.AbstractComponentManager.enable(AbstractComponentManager.java:261)
at org.apache.felix.scr.impl.config.ImmediateComponentHolder.enableComponents(ImmediateComponentHolder.java:328)
at org.apache.felix.scr.impl.BundleComponentActivator.initialize(BundleComponentActivator.java:158)
at org.apache.felix.scr.impl.BundleComponentActivator.<init>(BundleComponentActivator.java:113)
at org.apache.felix.scr.impl.Activator.loadComponents(Activator.java:261)
at org.apache.felix.scr.impl.Activator.bundleChanged(Activator.java:179)
at org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:868)
at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:789)
at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:514)
at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4319)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1993)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:947)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:934)
at org.apache.felix.webconsole.internal.core.BundlesServlet.doPost(BundlesServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.felix.webconsole.internal.servlet.OsgiManager.service(OsgiManager.java:493)
at org.apache.felix.webconsole.internal.servlet.OsgiManager.service(OsgiManager.java:418)
at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:127)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:179)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.felix.http.sslfilter.internal.SslFilter.doFilter(SslFilter.java:55)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:259)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:75)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
Caused by: java.lang.ClassNotFoundException: javax.xml.ws.BindingProvider not found by com.adobe.cq.customer-bundle [300]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1499)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1882)
at java.lang.ClassLoader.loadClass(Unknown Source)
pom.xml-
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>com.adobe.cq.customer-bundle</Bundle-SymbolicName>
<Export-Package>
com.adobe.cq.*;version=${project.version}
javax.xml.ws*;version=2.1;-split-package:=merge-first;-noimport:=true
</Export-Package>
<Private-Package>
javax.jws,
javax.jws.*,
javax.wsdl,
org.xml.*,
org.apache.cxf,
org.apache.cxf.*,
javax.xml,
org.apache.servicemix.specs.locator;-split-package:=merge-first
javax.xml.transform.stax,
javax.net.ssl,
org.w3c.dom,
org.apache.ws.commons.schema.resolver.*,
org.apache.ws.commons.schema.extensions.*,
org.apache.ws.commons.schema.*,
net.webservicex
</Private-Package>
<Include-Resource>{maven-resources}</Include-Resource>
<Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>
org.osgi.framework,
org.osgi.service.component.*,
com.sun.msv.*;resolution:=optional,
com.sun.xml.bind.marshaller.*;resolution:=optional,
com.sun.xml.fastinfoset.stax.*;resolution:=optional,
net.sf.cglib.*;resolution:=optional,
org.apache.aries.*;resolution:=optional,
org.apache.axiom.*;resolution:=optional,
org.apache.mina.*;resolution:=optional,
org.apache.log4j.spi.*;resolution:=optional,
org.apache.velocity.*;resolution:=optional,
org.osgi.service.blueprint.*;resolution:=optional,
org.junit.*;resolution:=optional,
org.relaxng.*;resolution:=optional,
org.slf4j.spi.*;resolution:=optional,
org.springframework.*;resolution:=optional,
javax.resource.*;resolution:=optional,
javax.mail.*;resolution:=optional,
javax.xml.ws.spi.http.*;resolution:=optional,
junit.framework.*;resolution:=optional,
com.sun.*;resolution:=optional,
sun.*;resolution:=optional,
org.apache.*;resolution:=optional,
org.jvnet.*;resolution:=optional,
javax.net.*,
javax.xml.transform.stax,
!*
</Import-Package>
</instructions>
</configuration>
</plugin>
In my case I needed to roll back to Java 8 from Java 11 on this project.
I know this is not a long term solution, but it might be helpful for someone.
The more general solution would be to use:
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.1</version>
</dependency>
This error happens when CXF delegates to the JDK for the proxy generation. The problem there is that for all interfaces the proxy has to implement you can only set one classloader. Typically users do not have the BindpProvider in their classpath. SO cxf uses the bus classpath which looks in the bundle classpath first and then in the cxf classpath.
Which CXF version do you use? In older CXF versions this error was misleading sometimes. It was not only issues when the BindingProvider class was not found. It also happened when BindingProvider was loaded by cxf and the user bundle but from different classloaders.
The best way to avoid this problem is to import javax.xml.ws. So you have the highest chance you find the same interface as cxf. If the error then still happens you might have two bundles exporting the package.
In any case your bundle plugin instruction should not look as complicated as in your question. This only leads to problem. Just try.
<instructions>
<Bundle-SymbolicName>com.adobe.cq.customer-bundle</Bundle-SymbolicName>
<Include-Resource>{maven-resources}</Include-Resource>
<Import-Package>
javax.xml.ws,
*
</Import-Package>
</instructions>
Is there any special reason why you embed all your dependencies? CXF typically does work to well when used like this. Instead you can use the bundles from the CXF DOSGi multi bundle distro to install CXF into CQ.
add javax/xml/ws/BindingProvider class to your classpath
If you are using maven, here a quick link to the possible artifacts
I'd say check your MANIFEST.MF first. It is complaining it's missing javax.xml.ws packages, so check if it is there in the Import-Package header.
If it isn't: Thats your problem, I guess you need to add it to your pom.xml.
If it is there, it's a bit more subtle.
In case you have installed the JDK 1.6 (Java SE 6) I suggest you read the document Using JAX-WS 2.x / Metro 1.x/2.0 with Java SE 6
I solved a similar problem just by reading the attached document.

JUnit, JPA, Hibernate and Postgres: How to test?

I've been stuck for a while now. I have searched a lot and I can't find the easiest way to test entity classes or JPA operations against a postgres database. I've found how to using Spring, Mockito and other things, but I can't find the simplest way using pure Java.
I have the following JUnit test:
public class ModelConverterTest {
private static EntityManagerFactory emf;
private static EntityManager em;
public ModelConverterTest() {
}
#BeforeClass
public static void setUpClass() throws Exception {
emf = Persistence.createEntityManagerFactory("PU");
em = emf.createEntityManager(); // Retrieve an application managed entity manager
}
#AfterClass
public static void tearDownClass() throws Exception {
em.close();
emf.close(); //close at application end
}
#Before
public void setUp() {
...
}
#After
public void tearDown() {
}
/**
* Test of SIMModelToModel method, of class ModelConverter.
*/
#Test
public void testSIMModelToModel() {
System.out.println("SIMModelToModel");
SIMModel simModel = new PESMModel();
simModel.addState(testState);
Model expResult = null;
Model result = ModelConverter.SIMModelToModel(em, simModel);
assertTrue(expResult!=null);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
}
and when running it, I get the following error:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/PersistenceContextType
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getDeclaredMethods(Class.java:1808)
at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:104)
at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:101)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:100)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:84)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:221)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
at java.lang.reflect.Field.declaredAnnotations(Field.java:1033)
at java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1026)
at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:196)
at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:26)
at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:52)
at org.junit.runners.model.TestClass.<init>(TestClass.java:45)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:51)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
My persistence.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/modelsystemdb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
What should I do to make the test run?
Quoting Trouble With Crippled Java EE 6 APIs in Maven Repository And The Solution by Adam Bien:
Instead of using
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
You should use alternative (geronimo, jboss etc.) dependencies:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.1_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
Had a similar issue. i.e Even though the Hibernate JPA jar was available through transitive dependencies and javaee 6.0 dependency was provided, the JUNIT Test case was failing. I just moved the javaee dependency at the end of the dependencies definition on the pom file so that the hibernate JPA api jar appeared before the Javaee jar during classpath resolution. That seemed to do the trick and I was able to run the test case. Hope this helps.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
javaee appears after hibernate dependency
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
If you want to use JPA in your tests, then you will need to have a JPA provider on the classpath when you run them. You can compile against javaee-api, but you must have a real, live provider at runtime.
You mention that you're using GlassFish; that uses EclipseLink as its provider, so it would make sense for you to do the same for your tests. There's information about using EclipseLink via Maven on their wiki.
We a very similar issue: Junit tests were successfully executing during maven build, however trying to run them from Eclipse directly showed this awful "Absent Code" error. The solution is not in pom.xml-s (they must be right since your build runs, right?) but the configuration of Junit execution in eclipse. In our case, when the maven dependencies were moved on top of the project itself, the correct implementation (eclipselink 2.0.4) persistence classes were loaded. So in eclipse try to check "Run Configurations...", select the Junit configuration in question and on the "Classpath" tab, change the order of libraries in the "User Entries" section: move Maven dependencies on top.
Best regards,
Joe Public
In my case under Run Configurations/ClassPath on the the project, click Edit and check "Only include exported entries" that did the trick.

Categories

Resources