Error while creating S3 Client in Java - java

I'm having a problem.
I have a Java project that works fine (written by someone else), I cloned it to my computer (from GitHub) and opened it in Eclipse. When I'm trying to run it, I keep getting an error when the script tries to create a s3 client.
This is the code (this.client = AmazonS3Client instance):
ClientConfiguration clientConfiguration = new ClientConfiguration();
ClientConfiguration.setMaxConnections(10);
clientConfiguration.setConnectionTimeout(120000);
clientConfiguration.setSocketTimeout(120000);
clientConfiguration.setMaxErrorRetry(5);
this.client = new AmazonS3Client(new InstanceProfileCredentialsProvider(), clientConfiguration);
This is the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.amazonaws.internal.config.InternalConfig.<clinit>(InternalConfig.java:43)
at com.amazonaws.internal.config.InternalConfig$Factory.<clinit>(InternalConfig.java:304)
at com.amazonaws.util.VersionInfoUtils.userAgent(VersionInfoUtils.java:142)
at com.amazonaws.util.VersionInfoUtils.initializeUserAgent(VersionInfoUtils.java:137)
at com.amazonaws.util.VersionInfoUtils.getUserAgent(VersionInfoUtils.java:100)
at com.amazonaws.ClientConfiguration.<clinit>(ClientConfiguration.java:64)
I tried also "this.client = AmazonS3ClientBuilder.defaultClient();" with a "Amazon3Client" instance, but it didn't work as well - same problem, error when trying to define ClientConfiguration...
I'm using aws-java-sdk-1.11.160.jar (tried before with 1.11.111 and didn't work, so I thought maybe upgrading the jar would help... it didn't)
I'm on this a couple of days now, and I can't seem to find the problem...
Any help would be great... Thank you!

You miss the jackson-databind jar in your classpath.
If you use maven, you can add the following dependency to solve this error:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version><your version></version>
</dependency>

This code is written to grab the AWS secret and access key from the EC2 instance profile. When you clone it locally you'll need to provide a different way to authenticate to the AWS API.
http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
When constructing the client this code is using the option for a IAM instance profile
new InstanceProfileCredentialsProvider(),
Credentials provider implementation that loads credentials from the Amazon EC2 Instance Metadata Service.
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/InstanceProfileCredentialsProvider.html

Related

NoSuchMethodError in com.google.api.client library

I'm trying to use the Google Cloud Storage SDK for Java into my Spring application.
Using Maven I've added it to my dependencies:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.31.0</version>
</dependency>
Then I try to initialize the Storage client with:
client = StorageOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new ClassPathResource("/my/path/to.json").getInputStream()))
.build()
.getService();
but an error occurs
java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient$Builder.setBatchPath
I've noticed that in my google-api-client-1.23.0.jar libray loaded as dependency no setBatchPath was present.
I've tried to find the issue browsing on web but without fortune.
I cannot understand which library (and version) I need to install in order to make it works.
Eventually I solved, there was a library duplication between my Web Application and Its Business Layer Jar component (I have a multi-module Project).
What I cannot understand is why there was a call to a non-existing method (AbstractGoogleJsonClient$Builder.setBatchPath) that was solved in this way.
In google-api-client-1.20.0.jar that method does not exists nor in google-api-client-1.23.0.jar
Try reading this, and then you may be able to work around the error.

Access EC2 Instance using Basic Credentials

I'm trying to use the Java SDK to programmatically stop an EC2 instance. I'm very new the AWS api, but what I'm trying to do should be very basic:
BasicAWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonEC2Client ec2 = new AmazonEC2Client(credentials); //ERROR caused by this line
I've seen several examples of this, for example here. I know the better way to do this is by using IAM roles, but I'm just trying to get something to work first. The line that creates the AmazonEC2 throws this error
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper at com.amazonaws.util.json.Jackson.
I can't figure out why it's throwing a class definition not found error.
I figured out the issue. Apparently the AWS library requires three more dependencies in fasterxml.jackson.core. Because I was using Ivy to configure these, I had to add these manually

ServiceBusConfiguration.configureWithConnectionString can't work, error "The key 'SharedAccessKeyName' is not valid for this connection string"

I'm using newest Azure java sdk(version 0.6), but I found I have problems when I use service bus configuration "configureWithConnectionString" function.
Here is my code below,
//get config
string connectionString = "Endpoint=sb://testservicebusnamespace.servicebus.windows.net/;SharedAccessKeyName=MySharedAccessKey;SharedAccessKey=<this is secret not show>";
config = new Configuration();
ServiceBusConfiguration.configureWithConnectionString(null, config, connectionString);
//create service
service = ServiceBusService.create(config);
And I get connection String from my service bus namespace portal in Azure. But When I run this code, it throws an exception "The key 'SharedAccessKeyName' is not valid for this connection string".
I don't know what's the problem, because I get the connection from Azure portal, and I've checked its content(the SharedAccessKeyName, SharedAccessKey), they are right.
So could anyone help me? Is it my problem or this SDK needs update(because I've heard portal already uses SAS, but SDK still uses ACS to authenticate)?
Thanks very much.
It looks like Java Azure SDK(0.6.0) still uses ACS to authenticate. See this github issue comments:
the current SDK works with service bus namespace with ACS auth mode, but not new created servicebus namespace which use SAS token auth mode. we will investigate this issue
and
One workaround is to create the service bus namespace via PowerShell.
This seems to enable the ACS auth mode by default. Command is:
New-AzureSBNameSpace -Name MyNameSpace -Location "West Europe"
Take a note of the "DefaultKey" you get after running the command. You
need to use that with the ServiceBusConfiguration and I'm not sure if
that is available via the Azure management portal. Use "owner" as the
authenticationName parameter to the configureWithWrapAuthentication.
I'm adding this answer, because I thought I was doomed. But found a new library.
This dependency (which 0.5.0 was latest version when I wrote this post)......gives you the issue.
<dependency>
<groupId>com.microsoft.windowsazure</groupId>
<artifactId>microsoft-azure-api-servicebus</artifactId>
<version>0.5.0</version>
</dependency>
Microsoft slightly altered the groupid. Here is a dependency that seems much more update to date.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>0.9.7</version>
</dependency>
Maven link here : https://search.maven.org/#artifactdetails%7Ccom.microsoft.azure%7Cazure-servicebus%7C0.9.7%7Cjar
So its an old question. But hopefully this points somebody in the right direction.

How to connect XMPP bosh server using java smack library?

I have working xmpp client on webapp using strophe.js ,as per my use case scenario i have to switch to different pages rapidly
Current approach is not secure as jid and password is visible in java script ,I was finding work around to implement security in strophe client an trying to make connection time(with bosh) more shorter ,while going through the book "XMPP Programming with JavaScript and jQuery"by jake moffitt i came across one solution which element both of my above problems is to implement session mechanism.which says that we can use strophe attach(jid,sid,rid) to connect to existing connection,so i need SID and RID ,which i can get from application server!!!
book has given an example of automated connection to bosh server when user logged in the web application,author has implement it using an Django project in python,As I am using java as server side language i tried to implement same example using java smcak-4.0.3 and smack-bosh-4.0.3
but unable to connect to bosh server(i am using ejabberd as xmpp server)
my code is as below
BOSHConfiguration config = new BOSHConfiguration(false,"192.168.0.106",5280,"/http-bind/","192.168.0.106");
XMPPBOSHConnection xbc=new XMPPBOSHConnection(config);
xbc.connect();
xbc.login("admin", "admin");
System.out.println(xbc.getConnectionID());
stack trace
java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:352)
at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:347)
at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:155)
at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:67)
When i tried to login to bosh server it fails every time,i am not sure what is wrong here can some one explain me?
One more thing i have find is one can get session identifier(SID) using "xbc.getConnectionID()" but how to find request identifier?
Any help on above problem will be appriciable!!!!
thanks in advance!
I had a similar problem.
I donwload all the smack github I import smack.jar from /lib/ and add the 3 java files from /src/main/java/org/jivesoftware/smack/
I tried to fix it by importing smack-bosh-3.2.2-jar-with-dependencies.jar from /target/. I don't know why this diddn't work.
Finally, I read here that you need to download all the dependencies libraries :
jbosh-0.6.0.jar
xlightweb-2.5.jar
xSocket-2.4.6.jar
xpp3-1.1.3.3.jar
dom4j-1.6.1.jar
So I used smack.jar from /lib/ with all thoses libraries and this problem was solved.
As i said in comments, you need after to retreive RID. I used jbosh sources and add following lines :
In com.kenai.jbosh.BOSHClient class
//I introduced a new property
private Long rid;
//commented the following code
//long rid = requestIDSeq.getNextRID();
//and at that place added
this.rid = requestIDSeq.getNextRID();
//and finally added a new getter for rid
public Long getRid() {
return rid;}
Then in smack-bosh :
In BOSHConnection.java
public Long getRid() {
return client.getRid();}
public String getSid() {
return sessionID;}
Now I'm blocked cause my session is disconected. According to my openFire logs, it's because of overactivity. So I'm looking for a solution to reduce the number of presence messages.
To get latest benefits in Android BOSH you need Smack Releases 4.1.1
SMACK --- smack-tcp smack-sasl-provided smack-resolver-minidns smack-resolver-dnsjava smack-extensions smack-core smack-bosh smack-android-extensions smack-android
You additionally Need
XPP3 1.1.4c delete the javax package from the JAR file for android because android already have javax....QName class
JXMMP, JXMMP CACHE, JBOSH, MINIDNS, DNSJAVA, JSTUN, XBILL DNS
No need for Apache HTTP Client which is already available in android
Unfortunately it comes around some 12 JAR files,
better use Maven Android Project in Eclipse
Otherwise search the above keywords at Maven Central http://search.maven.org/#search and get the JAR files one by one
stack trace
java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:352)
at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:347)
at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:155)
at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:67)
The exception here clearly states that Webapp cant find class org.xmlpull.v1.XmlPullParserFactory. That sounds like you are missing xml-apis library in your classpath, (or some other implementation of XmlPullParser interface).

Getting CXF error: javax.xml.ws.WebServiceException: WSDL Metadata not available to create the proxy

I have used CXF's wsdl2java to generate code from a WSDL file. I then build the code using the ant build xml file (also generated by CXF's wsdl2java).
When I run my code on my local Java 7 machine, all is well. When I run the code on a linux box in the cloud running Java 1.5, I get the following error:
javax.xml.ws.WebServiceException: WSDL Metadata not available to create the proxy,
either Service instance or ServiceEndpointInterface com.a.b.TheService should have
WSDL information
I've had a search around and I can't find any information that might explain the error in my scenario. I'm not sure where to start on this one. Can anyone shed any light?
I mentioned Java versions above as it is an obvious difference, but it may have nothing to do with the problem.
UPDATE: Adding in the code below as a result of Syon's request:
private static final String servicesNamespace = "http://www.serviceprovider.com/services/2009/03/02";
private static final String servicesNamespaceSchema = "http://www.serviceprovider.com/services/2009/03/02/schema";
private static String SERVICE_NAME = "TheService";
private QName SERVICE_QNAME;
private TheService m_theService;
...
SERVICE_QNAME = new QName(servicesNamespace, SERVICE_NAME);
I wrote this code quite some time ago, and at the time I wrote the comment below.
I've included it here in case it is helpful:
// The sample code creates an instance of the generated TheService_Service class.
// TheService_Service has references to the local WSDL file that it was generated from, and
// will report an error if it is not found. To prevent that error, we could:
// (1) ensure that the WSDL is available locally in the production environment in the location
// referenced in the generated Java
// (2) generate the Java from the WSDL located on the web rather than local WSDL, meaning that
// the WSDL referenced in the generated Java would be a URL to where it is located on
// serviceproviders's web site.
// (3) Rather than create an instance of TheService_Service, just create an instance of its
// super class, javax.xml.ws.Service, which has a static method to create an instance of it that
// does not require the location of the WSDL to be passed to it.
// I am going to choose option (3). Option (2) is a close second.
Service service = Service.create(SERVICE_QNAME);
m_theService = service.getPort(TheService.class); <-- Fails here
((BindingProvider)m_theService).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
Binding binding = ((BindingProvider)m_theService).getBinding();
((SOAPBinding)binding).setMTOMEnabled(false);
Thanks,
Paul
To my knowledge, JAX-WS/CXF always requires the WSDL. Could it be that you have the WSDL included on the classpath somewhere on your local machine but not on your linux box?
Regardless, you should be able to fix this issue by using the Service.create(URL, QNAME) method. The URL needs to point to the WSDL, you can use either the web service endpoint + ?wsdl on the end, or save a copy of the WSDL locally and point to that instead.
In your comment you mention referencing the WSDL on the web being preferable. Personally I would store it locally as this will improve performance when calling the web service. The framework won't need to make a call over the network just to get the WSDL every time you create the proxy.
This happens if he web service uses authentication. The WSDL cannot be read until the user name and password have been entered...
Faced with the same problem, it turned out, I was missing these depedencies:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.0</version>
</dependency>
Just adding them to my classpath solved the problem.
Maybe you had them already in the classpath under Windows?

Categories

Resources