5

我正在尝试运行一个简单的 mapdb 示例,但出现错误:

    Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
    at org.mapdb.DBMaker.fileDB(DBMaker.kt)
    at leechies.Truc.main(Truc.java:9)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

我的课:

package leechies;
import java.util.concurrent.ConcurrentMap;

import org.mapdb.DB;
import org.mapdb.DBMaker;

public class Truc {
    public static void main(String[] args) {
        DB db = DBMaker.fileDB("file.db").make();
        ConcurrentMap map = db.hashMap("map").createOrOpen();
        map.put("something", "here");
        db.close();
    }
}

我的 pomx.xml

<dependencies>
    <dependency>
        <groupId>org.mapdb</groupId>
        <artifactId>mapdb</artifactId>
        <version>3.0.3</version>
    </dependency>

我运行 rigth click -> Run as... -> java application。

4

4 回答 4

8

要么kotlin-runtime必须进入classpath并使用 进行验证$ echo $CLASSPATH

或者你必须添加kotlin-runtime到 Maven 中,然后在 jar 内部组装mvn compile assembly:single

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-runtime</artifactId>
    <version>1.1.3</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>1.1.3</version>
    <scope>compile</scope>
</dependency>

这也需要附加到工件上,并且可以使用assembly-plugin.

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>event.handlers.InventoryEventHandler</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>

您可以通过以下方式验证 kotlin-runtime 是否已添加到 jar 中

$ jar -tf target/amz-wavelength-1.0-SNAPSHOT-jar-with-dependencies.jar | grep kotlin-runtime
META-INF/kotlin-runtime.kotlin_module

或者

$ jar -tf target/amz-wavelength-1.0-SNAPSHOT-jar-with-dependencies.jar | grep "kotlin/jvm/internal/*"
于 2017-06-24T07:51:17.057 回答
2

它将失败,因为您的类路径中没有必要的 kotlin 运行时 jar。您必须运行一些命令来解决此错误。请参阅此链接以获取命令:-

https://dzone.com/articles/exercises-in-kotlin-part-1-getting-started

于 2017-04-15T17:10:39.953 回答
2

@prayagupd 的回答对我有用。但我认为值得一提的是,另一种选择是使用maven-shade-plugin而不是maven-assembly-plugin(将其放在build/pluginspom.xml 文件的部分中):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

阴影插件很好,因为它允许您排除重复的类。如果您必须使用任何一个插件,很高兴知道您不需要两者。在我的快速测试中,构建时间和生成的 jar 文件大小几乎相同,但是程序集插件(prayagupd 建议的)不会在我的构建输出中添加一堆警告,所以我会这样做。

于 2018-07-11T16:01:07.643 回答
0

也许从 maven 运行你的类,它会添加所有必要的依赖项。

运行 Maven 项目的主类

于 2017-04-20T04:49:51.557 回答