Broken example in Google Drive SDK quick start documentation - java

I need to develop an application that would be able to download a given document from Google Drive. I have found out that the recommended technology to use in the Java world is currently the Google Drive SDK.
As described by the quick start guide I have imported the required dependencies to my Maven project like this:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v2-rev98-1.17.0-rc</version>
</dependency>
The official sample DriveCommandLine source code (to be found at the bottom of same page) refuses to compile with the following error messages:
The import com.google.api.client.json.jackson cannot be resolved (at DriveCommandLine.java, line 8),
JacksonFactory cannot be resolved to a type (at DriveCommandLine.java, line 27).
How do I fix it? I'm running Java version 1.7.

Apparently you need to import the following dependency as well in order for the code to work. It contains the JacksonFactory class.
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.16.0-rc</version>
</dependency>

Related

import issue for Google pubsub publisher code in java

I was reading the google document, i wanted to push my message into pubsub topic.
reference link:
https://cloud.google.com/pubsub/docs/quickstart-client-libraries#pubsub-client-libraries-java
Added the pom dependency as mentioned:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.82.0</version>
</dependency>
Example class used:
https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java
But after doing everything not sure why the import of all the class is not working through maven.
Error i have in IDE:
Also, when i checked my Java build path all the google related jars are look like this:
Add more on it, multiple time updated/clean/build the project, Restart the eclipse 100 times.
Literally I am not getting what to do now, can anybody help what I am missing here.
Appreciated your help in advance.

The import com.amazonaws.services.s3.AmazonS3ClientBuilder cannot be resolved

I am trying to access my s3 buckets from my java application, trying to implement this
https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-buckets.html
I've added the lib/aws-java-sdk-1.8.6.jar to my lib folder and added the dependency in my pom.xml as well.
I still get this import error
"The import com.amazonaws.services.s3.AmazonS3ClientBuilder cannot be resolved"
for "import com.amazonaws.services.s3.AmazonS3ClientBuilder;"
Whereas the imports
"import com.amazonaws.services.s3.AmazonS3;" and "import
com.amazonaws.services.s3.model.Bucket;" gave no errors.
Any help would be appreciated. I found some people trying to implement for Android ran in similar issues but not this exactly
I was having the same issue, and after some research I realized that AmazonS3ClientBuilder is not part from AWS Android SDK instead of that the class is part of AWS JAVA SDK so you have to include this on your dependencies:
implementation 'com.amazonaws:aws-java-sdk:1.11.404'
Definitely the dependency as mentioned in Amazon documentation did
not work. The path to the dependency does not exist, in Maven central repository.
"software.amazon.awssdk" % "aws-java-sdk" % "2.0.0"
The following too, did not resolve com.amazonaws.services.s3.AmazonS3ClientBuilder, though the JAR path is correct in Maven central -
"software.amazon.awssdk" % "aws-sdk-java" % "2.1.3"
I had to fallback to the following as mentioned in the previous comment.
"com.amazonaws" % "aws-java-sdk" % "1.11.465"
As I mentioned in my comment - Amazon recommends moving to V2:
The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.
All V2 examples have been thoroughly unit tested and work (V1 code examples are old).
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/s3
For people running Spring using Maven build, the pom.xml dependency you need to import com.amazonaws.services.s3.AmazonS3ClientBuilder; into the Java class is this:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.267</version>
</dependency>
The AWS Documentation is not correct, add this to your pom.xml file:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
<version>1.11.1005</version>
</dependency>
The AWS Documentation is not correct, add this to your pom.xml file:
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.15.32</version>
</dependency>

AndroidDriver giving ClassNotFoundException

I am still trying to get the example in this tutorial to work:
http://www.automationtestinghub.com/first-appium-test-script/
I am getting a ClassNotFoundException in the line:
AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(caps);
I have added java-client-6.0.0-BETA2.jar to my Build Path.
What am I missing?
This is not in any way connected to the question: How to resolve 'the import io cannot be resolved' - that part has been resolved.
Update 03/01/2018
I have converted my project to a Maven project.
Then I added the following to the pom.xml:
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.0.0-BETA2</version>
</dependency>
Then I ran Maven Build with Verify Clean.
The result was still the same:
ClassNotFoundException
Update 04/01/2018
Finally got this working.
I got a response from Appium support - “Selenium requires guava library to be in classpath.”
First I added the android version - but that gave a different error.
When I added the regular version - it all worked:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.6-jre</version>
<!-- or, for Android: -->
<version>23.6-android</version>
</dependency>
My advice would be to add maven framework support to your project.
If using IntelliJ do the following:
Right Click your folder structure and hit Add Framework Support
Then tick the Maven box and click OK to add this as a Maven Project.
You should then have a pom.xml generated within your folder structure open this.
Within this pom.xml you need to add the following code:
https://gist.github.com/michaelvoase/54f3bbf47b888bb004330a1d1b108068
This will then import the Appium library into your project.

Class org.apache.cxf.jaxrs.client.WebClient in CXF 3.0.7

I am having trouble in figuring out which jar does WebClient class belongs.
In CXF 2.7 it was in cxf-bundle.jar, but in 3.0.7 I am unable to find it. I've been looking up jars for 2 hours but couldn't get anything.
Thanks!
I'll post my comment as answer:
Please take a look at:
org/apache/cxf/cxf-rt-rs-client/3.0.7/cxf-rt-rs-client-3.0.7.jar
I found this JAR by searching http://mvnrepository.com/ for WebClient-Artifact ID (which i found in the API-Docs for CXF):
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
EDIT:
To find JAR's for specific packages, in most cases google is your friend:
Just google for (for example) org.apache.cxf.transport.servlet.CXFNonSpringServlet
But you can also search/analyze your local JAR's for this String (most ZIP-Tools support searching inside of ZIP/JAR-Files).
I was getting the error package org.apache.cxf.jaxrs.client does not exist,
Added dependecy
org.apache.cxf cxf-rt-rs-client 3.0.0-milestone1
issue resolved

java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

I have a Maven Java project that uses Mashape Unirest for sending HTTP requests to other URLs. I am currently writing an integration test (using TestNG) that sends a normal HTTP request using Unirest. When I run the integration test through Maven (via the Failsafe plugin), the request is sent out successfully. However, when I try to run the integration test via Eclipse, I keep on getting the following error:
FAILED: getCurrentTimeTest
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
at com.mashape.unirest.http.options.Options.refresh(Options.java:41)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:27)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:141)
at com.mashape.unirest.http.HttpClientHelper.requestAsync(HttpClientHelper.java:80)
at com.mashape.unirest.request.BaseRequest.asStringAsync(BaseRequest.java:56)
at ...
I am also able to reproduce this error using a basic Java application script.
I have made sure that the dependencies I am using in my pom.xml file are the latest and greatest, as seen below:
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
I have also checked out the source code of BasicLineFormatter.java, both from the source file downloaded to Eclipse and from Apache's Httpcore Github repo. In the Github repo, notice how the INSTANCE field is defined for the 4.3.x branch and the trunk branch, but not in older branches like 4.2.x. However, I am indeed using version 4.3.2 in my project, so I should be using a JAR file for Httpcore that has the latest version of BasicLineFormatter. I know that, based on the Maven Dependencies JAR files that are in my project, that I am indeed using the latest versions of these Apache dependencies, not the older versions specified as downstream dependencies of my project.
I have checked other various SOF and blog posts about this issue, such as Mashape Unirest Java : java.lang.NoClassDefFoundError and this blog post too, but they all seem to be talking about solving the NoSuchFieldError problem for Android. However, I'm dealing with a standalone Java application, not an Android application.
I am at a loss in determining how to troubleshoot this issue. Anyone have any idea what I need to do?
UPDATE
Instead of showing my test case, I will reduce the illustration of a reproduction of this problem to just a simple one-liner Java application, because the problem exists with any Java application or test case run through Eclipse, not just one particular test:
System.out.println(Unirest.get("http://www.google.com").asStringAsync().get().getBody());
Normally, this should print the HTML of the Google home page, but I instead get the NoSuchFieldError stack trace.
FIXED!
The problem was that the AWS SDK (it's on my classpath because I'm developing for Elastic Beanstalk) had a conflicting JAR file. Using Oleg's solution (thanks BTW), I printed the following output in a unit test:
jar:file:/some/path/aws-java-sdk/1.7.1/third-party/httpcomponents-client-4.2.3/httpcore-4.2.jar!/org/apache/http/message/BasicLineFormatter.class
I'll have to rearrange my classpath so that AWS SDK is no longer conflicting.
The only plausible explanation to this problem is there is an older version of HttpCore on the classpath (unless you also want to consider a possibility of green men from Mars messing with your computer remotely from a flying saucer).
You can add this snippet to your code to find out what jar the class gets picked up from. This might help find out why that jar is on your classpath in the first place.
ClassLoader classLoader = MyClass.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
This basically tells me that in my case the jar resides in the local maven repository and likely to have been added to the classpath by Maven
jar:file:/home/oleg/.m2/repository/org/apache/httpcomponents/httpcore/4.3.1/httpcore-4.3.1.jar!/org/apache/http/message/BasicLineFormatter.class
As already mentioned by previous comments, It's mainly because of the conflicting versions of httpcore jar, the static field INSTANCE is been added to BasicLineFormatter class in versions > 4.3.1, Though you might have added the latest version of the httpcore jar in your dependencies, but its highly possible that other (lower) version of jar is getting picked up.
So, first to confirm that, wrong jar is getting picked up, Use the following line of code -
ClassLoader classLoader = <Your Class>.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
If this prints, the lower version of the jar, then it's confirmed that it's picking the lower version of the httpcore jar (May be from other dependencies of your project),
Solution -
Add following maven/gradle dependencies at the top of dependency list (Or above the other project dependency which caused the conflict) -
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
I faced the same exception using unirest:
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at com.mashape.unirest.http.options.Options.refresh(Options.java:55)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:36)
And found it was due to DefaultConnectionKeepAliveStrategy.INSTANCE; and the conflicting jar was apache-httpcomponents-httpclient.jar in my classpath. Adding this post to help anyone who faces similar exception
I got this Exception: Caused by: java.lang.NoSuchFieldError: INSTANCE
Solution:
This happens if you have two different version classes in your classpath…. […], So I first find that class (one version of class), click that class, select build path, then I click remove from build path.
if you are using aws sdk this error occurs because of dependency mismatch.
To avoid this error do the following:
1.Put the dependecies in the required order aws sdk and the end preferably
2.Add shade plugin to the project
This solved my problem
you can refer to my answer in
HTTPClient Example - Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
my case is i have httpclient-4.4.1.jar, and httpcore-4.4.1.jar in my class path, but JVM loaded BasicLineFormatter from httpcore-4.0.jar

Categories

Resources