使用Kotlin开发Eclipse插件

是否可以使用Kotlin为eclipse开发插件?我正在从事的项目使用Maven Tycho和OSGi。

为防止误解,这与Kotlin Eclipse Plugin无关,而是与使用Kotlin语言开发自定义插件有关。

我在pom.xml中向Kotlin stdlib添加了一个依赖项,但这似乎还不够。

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

由于存在许多潜在的相关文件(MANIFEST.MFpom.xml,...),我认为仅链接GitHub存储库https://github.com/Tornac/slr-toolkit/tree/kotlin/plugins/de.tudresden.slr.questionnaire可能会更容易。

只有plugins/de.tudresden.slr.questionnaire中的代码是我的,我只关心将Kotlin代码添加到该特定插件中。我整合Kotlin的尝试可以在kotlin分支上找到。可以在此提交中找到所有更改:https://github.com/Tornac/slr-toolkit/commit/18f8f6de3a35644ef1ae595024667f4bdd10b604

在编译期间,将简要显示以下错误: ERROR: Cannot access built-in declaration 'kotlin.Unit'. Ensure that you have a dependency on the Kotlin standard library (5,14)

科特琳:

// 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();

调用doStuff()引起的Stacktrace:

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
book1928 回答:使用Kotlin开发Eclipse插件

一般来说,是有可能的。但是,它似乎只适用于使用 JDK 8 的旧版本 Eclipse。

我无法真正评论 Maven 构建,但是在没有 Maven 插件的情况下直接从 Eclipse 构建时,我面临着类似的问题。我做了相当广泛的研究以找到问题的根源。特别是,我在创建新插件项目时使用 Eclipse 提供的插件模板创建了一个最小设置。从模板创建这样一个项目,添加 Kotlin 特性并将其中一个类移植到 Kotlin 可靠地生成 java.lang.NoClassDefFoundError,而 IDE 不会抱怨任何错误。

查看 Kotlin 插件的开发人员文档,他们建议使用基于 Eclipse Oxygen 和 JDK 8 的设置。确实,我发现这适用于我的最小插件设置。我进行了更多测试,发现适用于我的设置的最新版本的 Eclipse 是带有 JDK 8 的 Eclipse 2020-06。较新版本的 Eclipse 需要 JDK 11 并显示 java.lang.NoClassDefFoundError 错误。我还用 JDK 11 尝试了 Eclipse 2020-06,它也显示了同样的错误。

请注意,这对于 Eclipse 中的插件开发来说似乎根本不是问题。它似乎是 Kotlin 插件以及开发混合 Java 和 Kotlin 应用程序的基本问题。在此处查看我的问题:Is ist possible to build mixed Kotlin and Java applications with a recent Eclipse and JDK11?

本文链接:https://www.f2er.com/3131078.html

大家都在问