Building unirest request without maven - java

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.

Related

Getting Exception in Data Driven Framework Scenario while using Apache POI

Getting Exception in Data Driven Framework Scenario while using Apache POI
I am getting the exception when I execute a scenario using datadriven framework with Apache POI. I am not sure if its a version issue, can anyone please take a look at the exception below to see what is causing this issue and any resolution. Let me know if need anymore details.
Versions:
Eclipse: Oxygen.3a Release (4.7.3a)
WebDriver: 3.4.0
POI: 3.17
As a work around I have added dom4j-1.6.jar and xmlbeans-xmlpublic-2.3.0.jar, which dint seem to solve the issue.
EXEPTION:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/impl/common/SystemCache
at org.apache.poi.POIXMLDocument.init(POIXMLDocument.java:76)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:62)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:295)
at Carpet_Utility.ExcelUtils.setExcelfile(ExcelUtils.java:24)
at Carpet_Main.Main1.main(Main1.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.impl.common.SystemCache
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
Exception Screenshot
I think you have downloaded the wrong jar xmlbeans-xmlpublic-2.3.0.jar.
You have to download the xmlbeans-x.x.x.jar.
If you are using maven, then add this dependency in the pom.xml :
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
or if you want to use jar, then download from here : mvnrepository
The screenshot of the org.apache.xmlbeans.impl.common.SystemCache class :
As i have already mentioned in a comments section that for apache poi functionality you have to add xmlbeans-2.3.0.jar dependency as well.
If you are using maven then you have to add this dependency in pom.xml along with dom4j-1.6.jar.
If you are using through jar dependency then you have to download this jar and then have to include in your project.

Got error while running Calendar Api Sample Code

Was trying to implement Google Calendar Code taken from https://developers.google.com/google-apps/calendar/quickstart/java but got an error as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
at CalendarQuickstart.authorize(CalendarQuickstart.java:76)
at CalendarQuickstart.getCalendarService(CalendarQuickstart.java:89)
at CalendarQuickstart.main(CalendarQuickstart.java:101)
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
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)
... 3 more
And the line was
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
Before this i just copied code from https://developers.google.com/google-apps/calendar/quickstart/java and created a Java Project and this two Jar file was missing
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
After importing these jar file got that error. Please help
The Google Calendar API Java Quickstart works fine. Based on the error message you received, it looks like you're missing one of the jetty dependencies. If you're using Gradle build (as suggested in here, check under your "Gradle Depenencies" for this jar file -jetty-6.1.26.jar.
If you're missing the jetty dependencies, Open your build.gradle and check to make sure you have included all the dependencies listed below.
dependencies {
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0'
compile 'com.google.apis:google-api-services-calendar:v3-rev125-1.20.0'
}
Finally, Build and run the Gradle file to download the missing dependencies. You should now be up and running ! :)
Good luck!

ClassNotFoundException in Camel blueprint test

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 !

NoClassDefFoundError in Osgi w/ Felix, ant

After a lot of work migrating our code to another & fixing all the manifests and the bundles,
when I try to run the applet on the server I get many errors like this one:
java.lang.NoClassDefFoundError: org/jitsi/service/configuration/ConfigurationService
at net.java.sip.communicator.service.resources.AbstractResourcesService.<init>(AbstractResourcesService.java:127)
at net.java.sip.communicator.impl.resources.ResourceManagementServiceImpl.<init>(ResourceManagementServiceImpl.java:48)
at net.java.sip.communicator.impl.resources.ResourceManagementActivator.start(ResourceManagementActivator.java:36)
at org.apache.felix.framework.util.SecureAction$Actions.run(SecureAction.java:1243)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:620)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.jitsi.service.configuration.ConfigurationService not found by [12515]
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:812)
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.felix.framework.ModuleImpl.getClassByDelegation(ModuleImpl.java:670)
at org.apache.felix.framework.resolver.WireImpl.getClass(WireImpl.java:102)
at org.apache.felix.framework.ModuleImpl.searchImports(ModuleImpl.java:1426)
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:747)
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
at java.lang.ClassLoader.loadClass(Unknown Source)
All the bundles are getting resolved properly, and even when I tried to get some code out of the external jar into our code, it still threw me that error, I know that's a problem in Felix that I need to fix, but I don't know where can I start, all the bundles starting in their proper time...
I'm building the app with ant/Felix.
Maybe it's a problem with the activator?
Thanks for all the help, love to hear some opinions...
Your bundle is using the class org.jitsi.service.configuration.ConfigurationService but it doesn't import the package. Make sure that org.jitsi.service.configuration is listed in the Import-Package header in your bundle's manifest.

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