java.lang.NoSuchFieldError: Companion when using okhttp3 and selenium - selenium

I'm using Retrofit2 and Okhttp to make HTTP calls in my project, that also uses Selenium. As soon as I added the following dependency:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.9.0</version>
</dependency>
I started to see the following error:
java.lang.NoSuchFieldError: Companion
at okhttp3.logging.HttpLoggingInterceptor$Logger$Companion$DefaultLogger.log(HttpLoggingInterceptor.kt:116)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:168)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
I have checked some related issues and everything points to a dependency problem. However, I'm using the latest versions for Okhttp and Retrofit2 (2.9.0), as well as for selenium-java:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
What I'm trying to do is just to log a simple request using:
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(url)
.client(httpClient.build())
.build();
Do you know what could be the issue?
I'm using the latest Kotlin plugin in IntelliJ

It's most likely a dependency problem. See which JARs are imported in your IDE then debug the error. Otherwise make a simple standalone android project to run this code. You can also use the okhttp bom to upgrade cleanly to 4.9.0.
https://github.com/square/okhttp#releases
dependencies {
// define a BOM and its version
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
// define any required OkHttp artifacts without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
}

Related

Got following error when try to execute first script to open web url with help of gecko driver -

Code is as below:
package nw;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.24.0-win64.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.shop.demoqa.com");
}
}
Hello i am facing this issue in selenium while launching the above given program --
program..plz help me
com/google/common/collect/ImmutableMap
at org.openqa.selenium.firefox.FirefoxDriver.<clinit>(FirefoxDriver.java:108)
at nw.test.main(test.java:14)
Caused by: java.lang.ClassNotFoundException:
com.google.common.collect.ImmutableMap
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
You have not added selenium-server.jar in your build or dependency in your POM.
You have added selenium-java.jar in your build or as dependency due to which your code is compiling fine
You need to add that too
Download jar from below location:
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server
OR add below dependency
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version>
</dependency>
Note that here I am using version 3.141.59 for server so your selenium java jar should of same version to prevent from any unrelated error
Selenium relies on Google Guava and your test is failing to find ImmutableMap class.
The solution is to add the relevant version of guava to your project classpath.
The full set of dependencies would be:
animal-sniffer-annotations-1.14.jar
byte-buddy-1.8.15.jar
checker-compat-qual-2.0.0.jar
commons-exec-1.3.jar
error_prone_annotations-2.1.3.jar
guava-25.0-jre.jar
j2objc-annotations-1.1.jar
jsr305-1.3.9.jar
okhttp-3.11.0.jar
okio-1.14.0.jar
selenium-api-3.141.59.jar
selenium-chrome-driver-3.141.59.jar
selenium-edge-driver-3.141.59.jar
selenium-firefox-driver-3.141.59.jar
selenium-ie-driver-3.141.59.jar
selenium-java-3.141.59.jar
selenium-opera-driver-3.141.59.jar
selenium-remote-driver-3.141.59.jar
selenium-safari-driver-3.141.59.jar
selenium-support-3.141.59.jar
Going forward I would recommend using a dependency management solution like Maven or Gradle so you won't have to manually download the .jars. Check out Selenium with Java article for comprehensive explanation and example code snippets.

IntelliJ Idea groovy.lang.GroovyRuntimeException: Conflicting module versions

My maven builds are fine and able to run groovy from cli. However if I try to run my groovy class inside IntelliJ Idea (version 15 community edition), its gives me below error.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.codehaus.groovy.runtime.InvokerHelper.<clinit>(InvokerHelper.java:61)
at groovy.lang.GroovyObjectSupport.<init>(GroovyObjectSupport.java:32)
at groovy.lang.Closure.<init>(Closure.java:219)
at groovy.lang.Closure.<init>(Closure.java:236)
at groovy.lang.Closure$1.<init>(Closure.java:203)
at groovy.lang.Closure.<clinit>(Closure.java:203)
at filter.App.<clinit>(App.groovy)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Caused by: groovy.lang.GroovyRuntimeException: Conflicting module versions. Module [groovy-all is loaded in version 2.3.9 and you are trying to load version 2.4.5
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl$DefaultModuleListener.onModule(MetaClassRegistryImpl.java:509)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromProperties(ExtensionModuleScanner.java:77)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromMetaInf(ExtensionModuleScanner.java:71)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModules(ExtensionModuleScanner.java:53)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:110)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:71)
at groovy.lang.GroovySystem.<clinit>(GroovySystem.java:33)
... 10 more
Not sure how to get rid of this.
This is my pom dependency.
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
</dependency>
And I am using spring boot.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath></relativePath>
</parent>
This is my groovy version installed in system.
Groovy Version: 2.4.5 JVM: 1.8.0_60 Vendor: Oracle Corporation OS: Linux
Please let me know if someone knows about this.
you have to delete groovy lib from project settings.
shift+alt+ctrl + s, global libraries - delete groovy. And when trying to run applictaion / test you should have point to groovy from maven dependencies.
#SuperAndrew's suggestion wasn't my situation -- I didn't have Groovy registered under Global Libraries under my Project Structure. But I did find this StackExchange solution resolved my issue. Add this code to your build.gradle file.
configurations.all {
resolutionStrategy {
force 'org.codehaus.groovy:groovy-all:2.4.4'
}
}
I was seeing this in a recent project after I upgraded Gradle to 7.+
The reason this was causing a problem for me was that Gradle 7.+ is now using Groovy 3.+ and as such various other plugins needed to be upgraded (e.g. spock-core).
It was this incompatibility of various dependencies that were causing this and once I updated all of them this issue went away.

Is there a jackson datatype module for JDK8 java.time?

I'm looking for a module for the new JDK8 java.time classes. I have looked through the FasterXML GitHub Project Listing and presently found none.
As I understand Jackson is still being compiled against JDK6 so can not use these classes directly and must have this built as a separate module, as was required with Joda.
I don't mind starting the project, though looking to see if any other efforts were already underway.
As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time.
Since Jackson 2.6.0 the "old" JSR310Module is deprecated. It is replaced by JavaTimeModule. Maven dependency is the same (you can find the current version in Maven Central):
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.6.0</version>
</dependency>
You have to register the module like this:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
Or like this:
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
Or like this (since 2.10 possible):
ObjectMapper mapper = JsonMapper.builder()
.findAndAddModules()
.build();
Note that as of 2.6, this module does NOT support auto-registration, because of existence of legacy version, JSR310Module. Legacy version has the same functionality, but slightly different default configuration: see com.fasterxml.jackson.datatype.jsr310.JSR310Module for details.
JavaTimeModule Source at GitHub
JavaTimeModule Usage
The most complete listing of datatype modules would be found from Jackson "portal" page at Github:
https://github.com/FasterXML/jackson
which does list "JSR-310: support for "Java 8 Dates".
Naming is bit opaque, but it refers to the standardization process, via JSR that should be finalized to produce the new Java8 Date API (if it's not already final; process has taken a while).
Direct link is: https://github.com/FasterXML/jackson-datatype-jsr310
You just need to import the jsr310 module.
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.5.0</version>
</dependency>
If module auto scan is not enable in your project, you will need to register it:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
If you use Spring Boot 2 and depends on the spring-boot-starter-web starter, you would have as dependencies both : the deprecated (jackson-datatype-jsr310) and the actual (jackson-datatype-jdk8).
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.0.M3:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.0.M3:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.0.pr4:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.0.pr4:compile
My bad, I was looking for a jackson-datatype-jdk8 as was for jackson-datatype-jdk7, however the project was actually created under the JSR name jackson-datatype-jsr310.
You should be able to use
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();

java.lang.ClassNotFoundException: com.google.api.client.json.JsonFactory

I want to create a web application running on Tomcat 7.0 with JRE JavaSE-1.6 on osx 10.8.
I am using a tutorial from the developers site and the error occurs when I try to call
clientSecrets = GoogleClientSecrets.load(new JacksonFactory(), reader);
I added the Jar google-http-client-jackson-1.16.0-rc.jar to my build path and still get following error:
java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
java.lang.ClassNotFoundException: com.google.api.client.json.JsonFactory
My classpath specifically points to this Jar too.
I just fixed this by changing the following lines:
Original: import com.google.api.client.json.jackson.JacksonFactory;
Modified: import com.google.api.client.json.jackson2.JacksonFactory;
I had a similar problem and solved it by manually adding the required JAR's to my WEB-INF\lib folder outside Eclipse.
From this page here, it says you need 3 libraries:
1) The Generated Java client library for BigQuery
2) The Google HTTP Client Library for Java
3) The Google OAuth Client Library for Java
Do you have them all? It sounds like you have #2, but it sounds like you're missing the google HTTP client.
In POM add below dependency-
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-gson</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.34.1</version>
</dependency>
Note: In above dependency version can be changed
2.Import statement
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
Usage
private static final JsonFactory JSON_FACTORY =
GsonFactory.getDefaultInstance();
GoogleClientSecrets clientSecrets =GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(in));

RabbitMQ tutorials code not working

I am in the process of learning RabbitMQ. I started with the basic rabbitmq tutorials in their website, unfortunately I am not able to compile them due to the following errors:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Error: "The method newConnection(Address[]) in the type ConnectionFactory is not applicable for the arguments ()"
Connection connection = factory.newConnection();
Error: The method newConnection(Address[]) in the type ConnectionFactory is not applicable for the arguments ()
The maven dependency I have is:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>rabbitmq-client</artifactId>
<version>0.9.1</version>
</dependency>
What exactly am I doing wrong here? Any help would be appreciated!
Thanks!
Your tutorial seems to be "old", try out a more up to date version. The current release of the RabbitMQ Java AMQP library is 3.1.3.. But also have a look at the Maven Repository. Try out version 3.1.1, it's the newest version in the mvn repo.
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.1.1</version>
</dependency>