【问题标题】:MapDB ClassNotFoundException: kotlin.jvm.internal.IntrinsicsMapDB ClassNotFoundException:kotlin.jvm.internal.Intrinsics
【发布时间】:2017-09-11 12:43:00
【问题描述】:

我正在尝试运行一个简单的 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>

我通过右击运行 -> 运行为... -> java 应用程序。

【问题讨论】:

标签: java maven kotlin noclassdeffounderror mapdb


【解决方案1】:

kotlin-runtime 必须在 classpath 中并使用 $ echo $CLASSPATH 进行验证。

或者你必须将kotlin-runtime添加到maven,然后用mvn compile assembly:single组装到jar本身,

<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/*"

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 它更好,但后来我得到:java.lang.NoClassDefFoundError: org/eclipse/collections/api/list/primitive/MutableLongList
    【解决方案3】:

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

    <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 建议的)不会在我的构建输出中添加一堆警告,所以我会这样做。

    【讨论】:

      【解决方案4】:

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

      run main class of Maven project

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多