Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchFieldError: Companion when packaged in WAR #51

Open
PeterTrotter opened this issue Sep 12, 2023 · 8 comments
Open

NoSuchFieldError: Companion when packaged in WAR #51

PeterTrotter opened this issue Sep 12, 2023 · 8 comments

Comments

@PeterTrotter
Copy link

PeterTrotter commented Sep 12, 2023

We've added sib-api-v3-sdk to an older project using Maven served on tomcat 7.

<dependency>
	<groupId>com.sendinblue</groupId>
	<artifactId>sib-api-v3-sdk</artifactId>
	<version>7.0.0</version>
</dependency>

When run with mvn clean tomcat7:run everything seems fine.

Packaged as a war mvn clean tomcat7:run-war it fails with:

Caused by: java.lang.NoSuchFieldError: Companion
	at okhttp3.internal.Util.<clinit>(Util.kt:70)
	at okhttp3.OkHttpClient.<clinit>(OkHttpClient.kt:1073)
	at sendinblue.ApiClient.<init>(ApiClient.java:80)
	at sendinblue.Configuration.<clinit>(Configuration.java:18)

Attaching a debugger and evaluating Configuration.getDefaultApiClient(); gives:

Method threw 'java.lang.NoClassDefFoundError' exception.
Could not initialize class sendinblue.Configuration
sendinblue.Configuration.getDefaultApiClient(Configuration.java:27)

Looking at mvn dependency:tree:

[INFO] +- com.sendinblue:sib-api-v3-sdk:jar:7.0.0:compile
[INFO] |  +- io.swagger:swagger-annotations:jar:1.5.18:compile
[INFO] |  +- com.squareup.okhttp3:okhttp:jar:4.10.0:compile
[INFO] |  |  +- com.squareup.okio:okio-jvm:jar:3.0.0:compile
[INFO] |  |  |  \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.5.31:compile
[INFO] |  |  \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.20:compile
[INFO] |  |     \- org.jetbrains:annotations:jar:13.0:compile
[INFO] |  +- com.squareup.okio:okio:jar:1.14.0:compile
[INFO] |  +- com.squareup.okhttp3:logging-interceptor:jar:4.10.0:compile
[INFO] |  |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.6.10:compile
[INFO] |  |     \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.6.10:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.8.9:compile
[INFO] |  +- io.gsonfire:gson-fire:jar:1.8.2:compile
[INFO] |  +- org.threeten:threetenbp:jar:1.3.5:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.0:compile
[INFO] |  \- org.apache.maven.plugins:maven-gpg-plugin:jar:1.5:compile
[INFO] |     +- org.apache.maven:maven-plugin-api:jar:2.0.6:compile
[INFO] |     +- org.apache.maven:maven-project:jar:2.0.6:compile
[INFO] |     |  +- org.apache.maven:maven-settings:jar:2.0.6:compile
[INFO] |     |  +- org.apache.maven:maven-profile:jar:2.0.6:compile
[INFO] |     |  +- org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
[INFO] |     |  |  +- org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
[INFO] |     |  |  \- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:compile
[INFO] |     |  \- org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
[INFO] |     +- org.apache.maven:maven-artifact:jar:2.0.6:compile
[INFO] |     +- org.apache.maven:maven-model:jar:2.0.6:compile
[INFO] |     \- org.codehaus.plexus:plexus-utils:jar:3.0.15:compile

All the jars are present in the war file WEB_INF/lib/ and there don't appear to be differing versions aside from those requested above.

Any suggestions?

It's a frustrating issue and it seems a waste of time to bypass your library and go direct to the API even though we would then get proper typing and far fewer dependencies ;-)

Targeting language level 1.8 on OpenJDK-11

@PeterTrotter
Copy link
Author

Please see https://github.com/PeterTrotter/brevo-tomcat-7 as a minimal example of the library failing when run packed in a war file on tomcat 7.

Any thoughts as to what might be causing this / solutions?

@PeterTrotter
Copy link
Author

PeterTrotter commented Sep 13, 2023

Could you confirm the okio version is as expected:

<dependency>
      <groupId>com.squareup.okio</groupId>
      <artifactId>okio</artifactId>
      <version>1.14.0</version>
 </dependency>

It seems to explicitly conflict with the transitive dependencies of com.squareup.okhttp3:okhttp:jar:4.10.0, specifically com.squareup.okio:okio-jvm:jar:3.0.0.

Is this strongly related to #50 ?

If okio is required it looks like the problem is that the release 6 months ago updated okhttp-version but not okio-version when the two are related.

@wilokecom
Copy link

wilokecom commented Sep 27, 2023

Please try this way to resolve it:
#1 Install okhttp package
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.1</version> </dependency>

#2 Init okthttp with @bean

@Bean
public OkHttpClient okHttpClient() {
	return new OkHttpClient.Builder()
			.readTimeout(60, TimeUnit.SECONDS)
			.connectTimeout(60, TimeUnit.SECONDS)
			.build();
}

@PeterTrotter
Copy link
Author

Thanks very much for your reply.

Is that the Spring @Bean annotation?

We are not using Spring and the example I gave is the simplest packaging possible of your library on Tomcat (Just 3 small files).

Please let me know which @Bean annotation it is so I can give it a try.

The cleanest fix I have found is to update your dependencies:

<!-- Brevo mailinglist integration -->
<dependency>
	<groupId>com.sendinblue</groupId>
	<artifactId>sib-api-v3-sdk</artifactId>
	<version>7.0.0</version>
	<exclusions>
		<exclusion>
			<groupId>com.squareup.okio</groupId>
			<artifactId>okio</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>com.squareup.okio</groupId>
	<artifactId>okio</artifactId>
	<version>3.0.0</version>
</dependency>

@ilyukou
Copy link

ilyukou commented Mar 19, 2024

Hi all,

Faced the same issue with Spring Boot.

   @Bean
    public ApiClient apiClient(@Value("${brevo.api-key}") String brevoApiKey) {
        ApiClient defaultClient = new ApiClient();
        ApiKeyAuth apiKey = (ApiKeyAuth) defaultClient.getAuthentication("api-key");
        apiKey.setApiKey(brevoApiKey);
        return defaultClient;
    }

Issue is throws on the first line new ApiClient()

Caused by: java.lang.NoSuchFieldError: Class okio.Options does not have member field 'okio.Options$Companion Companion'
	at okhttp3.internal.Util.<clinit>(Util.kt:70)
	at okhttp3.OkHttpClient.<clinit>(OkHttpClient.kt:1073)
	at sendinblue.ApiClient.<init>(ApiClient.java:80)

Fixed by adding maven dependency (#51 (comment))

@xehpuk
Copy link

xehpuk commented Jul 16, 2024

Funnily, Mailjet does have a similiar problem: mailjet/mailjet-apiv3-java#184

@Rapster
Copy link

Rapster commented Aug 21, 2024

Problem is still present. Fix as of today is:

    <dependency>
      <groupId>com.sendinblue</groupId>
      <artifactId>sib-api-v3-sdk</artifactId>
      <version>7.0.0</version>
      <exclusions>
        <exclusion>
          <groupId>com.squareup.okio</groupId>
          <artifactId>okio</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.squareup.okio</groupId>
      <artifactId>okio</artifactId>
      <version>3.6.0</version>
    </dependency>

Any chance to make a new minor release to fix that issue plz? @sendinblue @getbrevo

I'd be happy to make a PR if you can make that happen 😄

@masqueradezero
Copy link

solved by adding this into dependencies

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp-urlconnection</artifactId>
        <version>4.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>2.8.0</version>
    </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants