我想在Kotlin中引用Java的jar包。我尝试了以下方法,但是出现了错误。
javac -encoding utf-8 javasorce/test/JavaTestClass.java
jar cvf javasorce/test/JavaTestClass.jar javasorce/test/JavaTestClass.class
kotlinc kotlin/CallJavaTestClass.kt -cp javasorce/test/JavaTestClass.jar -include-runtime -d kotlin/CallJavaTestClass.jar
java -jar kotlin/CallJavaTestClass.jar
错误是:
Exception in thread "main" java.lang.NoClassDefFoundError:
javasorce/test/JavaTestClass at CallJavaTestClassKt.main(CallJavaTestClass.kt:5)
Caused by: java.lang.ClassNotFoundException: javasorce.test.JavaTestClass
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我正在使用以下目录:
root
|-javasorce
| |-test
| |-JavaTestClass.java
|-kotlin
|-CallJavaTestClass.kt
请告诉我是否有解决方案。
java CallJavaTestClassKt -cp kotlin/CallJavaTestClass.jar:javasorce/test/JavaTestClass.jar
而是java -cp kotlin/CallJavaTestClass.jar:javasorce/test/JavaTestClass.jar CallJavaTestClassKt
。在你的建议中,java
找不到 CallJavaTestClassKt。 - naokichi