ClassNotFoundException in Camel blueprint test - java

I am getting up to speed with the FUSE and OSGi and trying to put together a simple blueprint test that should check a Camel route (written by another developer):
public class CamelBrokerTest extends CamelBlueprintTestSupport {
#Override
protected String getBlueprintDescriptor() {
return "OSGI-INF/blueprint/peoplesoft-service.xml";
}
#Test
public void testRoute() throws Exception {
MockEndpoint endpoint = getMockEndpoint("seda:peopleSoftFinanceSchedulerProcessInstances");
endpoint.expectedMessageCount(1);
template.sendBody("activemq:peopleSoftFinanceIncomingFiles", "TRANSACTION_INFO");
assertMockEndpointsSatisfied();
}
}
However, when I try to run it, I end up getting
Caused by: java.lang.ClassNotFoundException: ca.custom.camel.servlet.registry.CamelServletHTTPRegistry
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)
... 40 more
The dependency containing the class is included in the POM file with the test scope:
<dependency>
<groupId>ca.custom</groupId>
<artifactId>camel-servlet-osgi</artifactId>
<version>${camel.servlet.osgi.version}</version>
<scope>test</scope>
</dependency>
And I verified that the file is in the repo, and actually contains the required files. It also looks like the generated OSGi bundle descriptor has the correct dependencies also:
Import-Package: ca.custom.camel.servlet.osgi;version="[1.1,2)",ca.
custom.camel.servlet.registry,ca.mcmaster.uts.service.directory.i
ntegration;version="[1.2,2)",javax.jws,javax.jws.soap,javax.net.ssl,jav
ax.security.auth.callback,javax.xml.bind,javax.xml.bind.annotation,java
This code seems to be running well in production, yet, fails when executed as a JUnit test. Any ideas as to why this exception might occur? Did I miss any configuration steps?

ClassNotFoundException is as far as I know the way of osgi to enforce you to use only packages and bundles that you have declared as dependencies in the manifest file so what I would check is the export packages of the bundle containing the class you are trying to use and see if it matches with the imports that you are using! Another thing is that the bundle you are trying to use might not be activated yet !

Related

Building unirest request without maven

I'm been trying to learn unirest and apparently, I'm stuck with the situation of learning how to compile it without using maven. Below is what I've learned so far since most of the tutorials I've found is teaching unirest with maven. I'm not sure what else I missed but this is what I got so far:
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
public class MainMethod {
public static void main (String argsp[]) throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://www.mocky.io/v2/5bc4373c300000b8097587bd")
.header("accept", "applicaiton/json").queryString("apiKey","123")
.asJson();
System.out.println(jsonResponse.getBody());
}
}
Each time I try to run the source code, I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpRequest
I'm just trying to create a simple request just to learn the basics of unirest but I'm not doing any good so far.
Did I forgot to do something? Any suggestion will be welcomed. Thanks.
EDIT:
This is the source code of the tutorial that I am following:
#Test
public void shouldReturnStatusOkay() {
HttpResponse<JsonNode> jsonResponse
= Unirest.get("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154")
.header("accept", "application/json").queryString("apiKey", "123")
.asJson();
assertNotNull(jsonResponse.getBody());
assertEquals(200, jsonResponse.getStatus());
}
Since the tutorial is using a method named assertNotNull and assertEquals which I can't figure out what they are, I simply replaced it with a print in hopes that I'll see how it responds.
Also, I'm using this site as a mock web service I think. I don't if its useful to the problem but here it is:
https://www.mocky.io/
The whole error message I'm receiving:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/concurrent/FutureCallback
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at MainMethod.main(MainMethod.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.http.concurrent.FutureCallback
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)
... 2 more
You have to add all dependencies unirest uses to your project as direct dependencies.
Take a look at the unirest dependency tree here:
You miss httpcore-4.2.3.jarin your classpath. addd it and the error will be solved
Add dependency in you pom.xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
after running mvn install command your problem will be solved.
or without Maven
download a jar file of httpcore from link and add into your class path.

From command line - java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

OK. I know there are other questions like this one out there and this is not the first time that slf4j has kicked my butt. However, I have looked at my PATH in Environment Variables and below are the two slf4j jar files included in my PATH as well as my project dependencies.
C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar
C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar
This is what is in my pom file:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-ext</artifactId>
<version>1.7.13</version>
</dependency>
Other applications we are running contain slf4j jar dependencies in the IDE but they are not listed in the pom file. I am so confused about where to put what that I can't see straight.
This is what the application dependencies look like:
I can run the application from the IDE (Netbeans) but I get the following error when I try to run from command prompt.
C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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
Adding slf4j in my VM classpath and I still get the error.
C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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
Can somebody please help me figure out what I am doing wrong?
Should I be adding slf4j-api to my dependencies instead of slf4j-exe? Or even something else?
I guess whichever one I use, I should add it to my VM classpath.
Does it even need to be in my pom file?
------------------------------ EDIT ----------------------------------
I created a simple HelloWorld app to log my name. As soon as I added the Logger to my Hello class it was highlighted in red, so I added the slf4j-simple to my dependencies and slf4j-api came with it. But when I opened the pom file only the slf4j-simple was added:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.13</version>
</dependency>
When I run from the IDE, everything works well. But when I run from command line I still get the error:
C:\Users\pdl\Projects\HelloWorld\target>java -cp HelloWorld-1.0-SNAPSHOT.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.helloworld.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.helloworld.Hello.<clinit>(Hello.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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
Where's slf4j in your VM classpath? Try running as follows:
java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client
You're missing slf4j from your runtime. Hope that helps.
Maven, and thus the pom-file, are intended to build the classpath for you.
So yes, all depedencies you like to use should be in your pom file.
Regarding slf4j:
slf4j-api is a dependency that only defines an api (or interface). To make it work you also have to add an implementation. See here for explanation. So you have to add at least one more dependency. For example:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
Regarding slf4j-ext, i am quite sure you dont need it to simply log. Maybe you have more elaborate use cases.
To start your application from command line you have to run java with the -cp argument. The classpath is a list all used classes or archives containing classes. For applications with a lot of dependencies it can become quiet cumbersome to build it manually.
Use mvn dependency:build-classpath to let maven build this big string of jar-paths for you.
This concerns Java SE projects built in IntelliJ where slf4j - or any other library for that matter - is included (i.e. slf4j-api-1.7.26.jar => The API & slf4j-simple-1.7.26.jar => the API implementation)...
Step #1:
create folder \libs in project root and place the two JARs in there click to see project structure
step #2:
Build code and make sure that the Logger is working properly inside the IDE.
step #3:
Launch cmd and navigate using cd command inside the .\out directory up until the packages
of the main (check 1st line of Main.java). i.e.
cd C:\Users.....\out...\testcp
step #4
Do right click on sjf4j-api jar and copy path (path#1) &
Do right click on sjf4j-simple jar and copy path (path#2) &
Do right click on Main.class and copy path (path#3. SOS: EXCLUDE THE Main.class part).
Now, run java command with -cp flag as:
java -cp path#1;path#2;path#3; org.me.Main
That's it
Note: Eclipse users do not run into this problem since this is taken care of by using the build path utility.

Create a jar file using jdev that includes weblogic.jar

I've been having troubles lately in creating a jar file that can call a secured web service on Weblogic server using Jdev.
I've created a web service proxy which is handling the situation perfectly. My goal is to deploy this web service as a jar file so that I can use it in my other projects as a simple library.
I was able to deploy the project as a jar file, which in turn allowed me to use it's different methods to connect to the web service.
However, when I run the web service client on eclipse I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/xml/crypto/wss/provider/CredentialProvider
at WebServiceCaller.callGetCardDetailJar(WebServiceCaller.java:55)
at WebServiceCaller.main(WebServiceCaller.java:29)
Caused by: java.lang.ClassNotFoundException: weblogic.xml.crypto.wss.provider.CredentialProvider
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)
... 2 more
The problem may be fixed by finding the weblogic.jar file and including it in the eclipse build path, but is there a way to deploy a jar file with all the library dependencies included in it ?
There is a way to create a wlfullclient.jar (see oracle doc http://docs.oracle.com/cd/E13222_01/wls/docs103/client/jarbuilder.html ), but your class is not in it, it's rather in oracle.webservices.standalone.client.jar or in wls-api.jar or in weblogic.jar... it's rather confusing, I think Oracle never managed to simplify this jar dependency problem, actually in earlier versions of WebLogic things were a lot simpler!

Java: java.lang.NoClassDefFoundError: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager

I created a webservice using servlet and Tomcat 6.0.
I created a java project in eclipse to call that webservice using HTTP.
I added the following jar files
1. httpcore-4.0
2. httpclient-4.0
but while running the project im getting the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:75)
at WSCall.HttpUtilities.GetServerResponse(HttpUtilities.java:52)
at WSCall.ServiceCall.main(ServiceCall.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
... 3 more
Please help me to find the solution.
You still have to add common-logging dependency under your classpath. However, you may continue encounter other simaliar Exceptions due to lack other dependency. I suggest you use maven to manage the dependency so that you don't have to mannualy add dependencies to your classpath.
It seems your project doesn't contain the Apache Commons Logging library. Adding it to your classpath should do the trick.
This is caused by the missing of commons-logging.jar.To fix it, download the commons logging library and add it to your project.

Java: NoClassDefFoundError using Apache Commons CLI 1.2

I am using the Apache Commons CLI 1.2 to parse command line arguments in Java. I had run into the NoClassDefFoundError when trying to run my java class but solved it by fixing the class-paths.
Now I have the same exception but in regards to the actual commons-cli classes as is shown below:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cl i/CommandLineParser Caused by:
java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineP arser
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) Could not find the main class: com.amirsys.score.api.script.CMDContentPusher.
The only thing I could think of is to set the class path to the commons-cli jar but that did not work. I haven't been able to find a solution to fixing the NoClassDefFoundError for imported classes. I thought these were compiled into the .class files?
Commons CLI is not in the classpath at runtime. If you struggle to get the classpath right you can try to package your application as a single big jar file containing all its dependencies, including Commons CLI. There are many tools to achieve that (jarjar, onejar, Maven shade plugin...)

Categories

Resources