5

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
user3340459
  • 435
  • 5
  • 13
  • I think that's possible because [it is possible to create plug-ins in Scala](https://stackoverflow.com/questions/446389/how-to-write-eclipse-rcp-applications-with-scala) and because parts of the [Kotlin Plugin for Eclipse IDE](https://github.com/JetBrains/kotlin-eclipse) seem to be written in Kotlin. You could try to mimic the way Scala plug-ins are written or contact the developers/users of the Kotlin Plugin. – Emmanuel Chebbi Nov 10 '19 at 11:30

1 Answers1

1

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?

  • Did you manage to get it working with newer JDKs? What I'd like to avoid is losing weeks to setup the RCP/PDE project. – LppEdd Jul 18 '21 at 15:52
  • Unfortunately, no. My recommendation: if you need Eclipse then stay away from Kotlin. Looking at the recent activity in the GitHub repo of the Kotlin plugin, I don't expect any fixes soon. I found this [fork](https://github.com/bvfalcon/kotlin-eclipse-2021) of the plugin, which looks a bit more promising, but it also does not solve the problem (yet). – Christian Menard Jul 19 '21 at 08:09
  • 1
    Thanks! You can have a look at the IntelliJ plugin Eclipse PDE Partial. It's still really new but it might be a good alternative in a couple months. – LppEdd Jul 19 '21 at 08:24
  • Thanks, this could actually come in very handy for our project. – Christian Menard Jul 19 '21 at 08:57
  • it should get more popular, then it would attract contributions. PDE is a bit of a niche tho... – LppEdd Jul 19 '21 at 08:59