andregasser / bigbone

BigBone - A Mastodon Client Library for Java and Kotlin
https://bigbone.social
MIT License
61 stars 15 forks source link

okhttp3 runtime error #448

Open scroogemcfawk opened 4 months ago

scroogemcfawk commented 4 months ago

I'm currently developing a client for one opensource project (LUWRAIN, a platform for blind and partly-sighted people) and encountered a problem I can not solve. I'm not really sure if this issue relates to bigbone, but I don't know where to ask for help ether.

So, when I run my program from IDE everything works, but when LUWRAIN starts it, I get this:

java.lang.NoSuchFieldError: Companion
        at okhttp3.internal.Util.<clinit>(Util.kt:70)
        at okhttp3.internal.concurrent.TaskRunner.<clinit>(TaskRunner.kt:309)
        at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:41)
        at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:47)
        at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:471)
        at social.bigbone.MastodonClient$Builder.<init>(MastodonClient.kt:804)
        at com.github.scroogemcfawk.mastodon.api.Mastodon.initClient(Mastodon.kt:55)
        at com.github.scroogemcfawk.mastodon.api.Mastodon.<init>(Mastodon.kt:49)
        at org.luwrain.app.mastodon.StartingLayout.onMailAddress(StartingLayout.java:47)
        at org.luwrain.controls.WizardArea$WizardClickable.click(WizardArea.java:75)
        at org.luwrain.controls.WizardArea.onClick(WizardArea.java:260)
        at org.luwrain.controls.WizardArea.onInputEvent(WizardArea.java:272)
        at org.luwrain.app.base.LayoutBase$1.onInputEvent(LayoutBase.java:276)
        at org.luwrain.core.EventDispatching.lambda$onInputEvent$2(EventDispatching.java:150)
        at org.luwrain.core.Base.unsafeAreaOperation(Base.java:288)
        at org.luwrain.core.EventDispatching.onInputEvent(EventDispatching.java:138)
        at org.luwrain.core.EventDispatching.onEvent(EventDispatching.java:80)
        at org.luwrain.core.Base.eventLoop(Base.java:111)
        at org.luwrain.core.Core.run(Core.java:70)
        at org.luwrain.core.Launch.run(Launch.java:108)
        at org.luwrain.core.Init.main(Init.java:75)

I assume there is a problem with okhttp3 library version.

I checked the sources of your library, there is okhttp3 version 4.12.0 specified. I checked what classes are loaded in LUWRAIN process (with -Xlog:class+load) when it starts my application, it says that only okhttp3 version 4.12.0 jar is loaded.

The whole project is built with maven + ant, so there is a lot of places that can may cause problems, but I excluded other versions in ant build file (because okhttp version 2* appears in the libraries of LUWRAIN sometimes), still didn't help.

The build file looks like this:

<project name="luwrain-app-mastodon" default="jar" basedir=".">
    <!-- todo#smf add property -->
    <typedef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="/home/smf/opt/kotlinc/lib/kotlin-ant.jar"/>
    <path id="classpath">
        <pathelement location="."/>
        <fileset dir="../release/lib">
            <include name="**/*.jar"/>
            <exclude name="**/*okhttp-2*.jar"/>
            <exclude name="**/*okhttp-3*.jar"/>
            <exclude name="**/*okhttp-4.3*.jar"/>
        </fileset>
        <fileset dir="../release/jar">
            <include name="**/*.jar"/>
            <exclude name="**/*okhttp-2*.jar"/>
            <exclude name="**/*okhttp-3*.jar"/>
            <exclude name="**/*okhttp-4.3*.jar"/>
        </fileset>
    </path>
    <path id="mainjar">
        <pathelement location="."/>
        <fileset dir="jar">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <target name="clean">
        <delete dir="build"/>
        <delete dir="jar"/>
        <delete dir="target"/>
    </target>
    <target name="compile" depends="clean">
        <mkdir dir="build"/>
        <mkdir dir="build/main"/>
        <javac includeantruntime="false" encoding="UTF-8" srcdir="src/main/" destdir="build/main" source="17"
               target="17" debug="true" debuglevel="lines,vars,source">
            <classpath refid="classpath"/>
            <compilerarg value="-verbose"/>
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-Xlint:-options"/>
            <withKotlin/>
        </javac>
    </target>
    <target name="jar" depends="compile">
        <mkdir dir="jar"/>
        <jar jarfile="jar/luwrain-app-mastodon.jar">
            <manifest>
                <section name="org/luwrain">
                    <attribute name="Extensions" value="org.luwrain.app.mastodon.Extension"/>
                </section>
            </manifest>
            <fileset dir="./build/main">
                <include name="**/*.class"/>
            </fileset>
            <fileset dir="./src/main/resources">
                <include name="**/*"/>
            </fileset>
        </jar>
    </target>
    <target name="compile-test" depends="jar">
        <mkdir dir="build"/>
        <mkdir dir="build/test"/>
        <javac srcdir="src/test/java" destdir="build/test" source="1.8" target="1.8">
            <classpath>
                <path refid="classpath"/>
                <path refid="mainjar"/>
            </classpath>
        </javac>
    </target>
    <target name="jar-test" depends="compile-test">
        <jar jarfile="jar/luwrain-app-mastodon-test.jar">
            <fileset dir="./build/test">
                <include name="**/*.class"/>
            </fileset>
        </jar>
    </target>
    <target name="junit" depends="jar-test">
        <junit printsummary="withOutAndErr">
            <classpath>
                <path refid="classpath"/>
                <path refid="mainjar"/>
            </classpath>
            <formatter type="xml" usefile="true"/>
            <batchtest fork="yes">
                <fileset dir="build/test">
                    <include name="**/*Test.class"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

The stack trace changes, if I use okhttp3 libraries of different versions (3.14.9, 4.3.1).

I tried everything I could try and at this point I don't have any ideas how to solve this problem.

andregasser commented 4 months ago

Hi @scroogemcfawk

From a high level perspective, what I would do is probably something like this:

You could also try to enforce a specific version for okhttp in the whole project. Eventually, you could try to set the version to 4.12. Maybe this helps: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#transitive-dependencies

Also, removing dependencies that include okhttp one by one and test if it runs could help to narrow down the issue a bit. Simplify the project and narrow down the root cause.

Running on three different major versions of okhttp is calling for trouble imho. I would also try to stay at one major if possible (which ideally is 4.x).

Sorry, that I have no better answer at the moment.

Good luck!

andregasser commented 4 months ago

Just found this after a quick google: A guy had a very similar problem as you and they had to override the okhttp version in their case. Maybe you're suffering from the same problem: https://stackoverflow.com/questions/65828761/java-lang-nosuchfielderror-companion-when-using-okhttp3-and-selenium

scroogemcfawk commented 4 months ago

Hi @andregasser, thank you for the advices above! I've consulted with the maintainer of the LUWRAIN project. Most likely, there is indeed a version clash of okhttp library, we decided to postpone the build of the whole project. So, when the time comes, we will fix this problem and I will update this issue with the solution.