Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Kotlin to develope an Eclipse plugin

Is it possible to use Kotlin to develop a plugin for Eclipse? The project I'm working on uses Maven Tycho and OSGi.

To prevent misunderstandings, this is not about the Kotlin Eclipse Plugin, but about developing a custom plugin using the Kotlin language.

I added a dependency to the Kotlin stdlib in my pom.xml, but that doesn't seem to be enough just yet.

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>${kotlin.version}</version>
</dependency>

Since there are a lot of potentially relevant files (MANIFEST.MF, pom.xml, ...) I thought it might be easier to just link the GitHub repo: https://github.com/Tornac/slr-toolkit/tree/kotlin/plugins/de.tudresden.slr.questionnaire.

Only the code in plugins/de.tudresden.slr.questionnaire is mine, and I only care about adding Kotlin code to that particular plugin. My attempt to integrate Kotlin can be found on the kotlin branch. All the changes can be found in this commit: https://github.com/Tornac/slr-toolkit/commit/18f8f6de3a35644ef1ae595024667f4bdd10b604

During compilation, the following error is briefly shown: ERROR: Cannot access built-in declaration 'kotlin.Unit'. Ensure that you have a dependency on the Kotlin standard library (5, 14).

Kotlin:

// file: KtTest
package de.tudresden.slr.questionnaire

class KtTest {
    fun doStuff() {
        System.out.println("hello world, this is kt!");
    }
}

Java:

// trying to call KtTest's method from Java
new KtTest().doStuff();

Stacktrace caused by calling doStuff():

java.lang.NoClassDefFoundError: de/tudresden/slr/questionnaire/KtTest
    at de.tudresden.slr.questionnaire.QuestionnaireView$1.mouseDown(QuestionnaireView.java:78)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:192)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:694)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:606)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at de.tudresden.slr.app.Application.start(Application.java:20)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Caused by: java.lang.ClassNotFoundException: de.tudresden.slr.questionnaire.KtTest cannot be found by de.tudresden.slr.questionnaire_0.2.6.qualifier
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 29 more
like image 931
user3340459 Avatar asked Nov 06 '22 12:11

user3340459


1 Answers

Generally speaking, it is possible. However, it appears to only work for older versions of Eclipse using JDK 8.

I cannot really comment on the Maven build, but I am facing a similar problem when building directly from Eclipse without a Maven plugin. I did quite extensive research to find the root of the problem. In particular, I created a minimal setup using the plugin templates provided by Eclipse when creating a new Plug-in project. Creating such a project from a template, adding the Kotlin nature and porting one of the classes to Kotlin reliably produces java.lang.NoClassDefFoundError, while the IDE does not complain about any errors.

Looking at the developer documentation of the Kotlin plugin, they suggest using a setup based on Eclipse Oxygen and JDK 8. Indeed, I found this to work for my minimal plugin setup. I did more tests and found the latest version of Eclipse that works for my setup is Eclipse 2020-06 with JDK 8. Newer versions of Eclipse require JDK 11 and show the java.lang.NoClassDefFoundError error. I also tried Eclipse 2020-06 with JDK 11, which also shows the same error.

Note that this appears to be not at all a problem with plugin development in Eclipse. It rather seems to be a fundamental problem of the Kotlin plugin and with developing mixed Java and Kotlin applications. See my question here: Is ist possible to build mixed Kotlin and Java applications with a recent Eclipse and JDK11?

like image 88
Christian Menard Avatar answered Nov 12 '22 19:11

Christian Menard