本文介绍了Android Studio上的Kotlin(非Android)项目的ClassNotFoundException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Android Studio 2.3.3创建一个正常的Kotlin项目(非android,只是桌面/命令行项目)。



我遵循以下步骤:这里列出了



和以下是我有的项目文件结构





然后我同步该项目,它会成功。但是当我尝试运行项目时出现错误。

 /应用程序/ Android Studio.app/Contents/jre/jdk/Contents/Home/bin / java-Didea.launcher.port = 7534-Didea.launcher.bin.path = / Applications / Android Studio.app/Contents/bin-Dfile.encoding = UTF-8 -classpath/ Applications / Android Studio。 app / Contents / jre / jdk / Contents / Home / jre / lib / charsets.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/jib/ext/cldrdata.jar:/Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/ jaccess.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/localedata.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/ jre / lib / ext / nashorn.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunec.jar:/Applications/Android Studio.app/Contents/jre/ jdk / Contents / Home / jre / lib / ext / sunjce_provider.jar:/ Applications / Android Stu dio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/zipfs。 jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/jce.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ jsse.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/management-agent.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/ jre / lib / resources.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/rt.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/ Home / lib / dt.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/lib/jconsole.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/ lib / sa-jdi.jar:/ Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/lib/tools.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/ org.jetbrains /注解/ 13.0 / 919f0dfe192fb4e063e7dacadee7f8bb9a267 2A9 /注释-13.0.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre7/1.1.4-2/272a21c30432c943d618008fbbd34762eb0d6c8a/kotlin-stdlib- jre7-1.1.4-2.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.1.4-2/fec983b2608a8a91a1b87dba18d247d692cf6b51/kotlin-stdlib- 1.1.4-2.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre8/1.1.4-2/168cc3f45c307edda6c867dd0635a7d451257551/kotlin-stdlib- jre8-1.1.4-2.jar:/ Applications / Android Studio.app/Contents/lib/idea_rt.jarcom.intellij.rt.execution.application.AppMain io.wasin.testjavaapp.Main 
Exception in线程mainjava.lang.ClassNotFoundException:io.wasin.testjavaapp.Main $ b $在java.net.URLClassLoader.findClass(URLClassLoader.java:381)
在java.lang.ClassLoader.loadClass(ClassLoader .java:424)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass( ClassLoader.java:357)在java.lang.Class.forName0处为
(本地方法)$ b $在java.lang.Class.forName(Class.java:264)
at com.intellij。 rt.execution.application.AppMain.main(AppMain.java:123)

处理完成退出代码1

我看到了上面提供的classpath参数 / Applications / Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java没有与我的项目相关的目录。我不知道如何通过gradle添加classpath目录,尽管我已经在配置中设置它了。



我该如何解决这个问题?



更新

以下是我的 Main.ct 代码。这只是为了测试目的。所以只是打印出来。

 包io.wasin.testjavaapp 

对象Main {
@JvmStatic fun main(arg:Array< String>){
println(Hello world)
}
}

此外,我还更新了Kotlin版本的 build.gradle 以匹配Android Studio上的Kotlin插件版本。现在是 ext.kotlin_version ='1.1.4-2'。虽然仍然无法解决问题。



更新2



在 build 目录中,在 src 目录的同一级别。没有 .class 或中间体在那里生成/构建的文件,只有 kotlin-build /caches/version.txt 就是这样。没有其他文件。所以这个问题可能是由于源文件根本不编译?



更新3



我试着创建一个新模块来查看它是否是根本原因。还创建了 assets 文件夹来充当工作目录,重新排列源文件夹,如下所示。




  1. testJavaApp - root one

  2. 桌面 - 由 Main.ct 并用作桌面可执行文件

testJavaApp 对于 build.gradle

具有以下代码:

  group'io.wasin'
version'1.0-SNAPSHOT'

//顶级构建文件,您可以在其中添加常见的配置选项
所有子项目/模块。
buildscript {
ext.kotlin_version ='1.1.4-2'
存储库{
jcenter()
}
依赖关系{
classpath 'com.android.tools.build:gradle:2.3.3'
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version

//注意:不要在这里放置您的应用程序依赖项它们属于单个模块build.gradle文件中的
//
}
}

allprojects {
存储库{
jcenter()



任务清理(类型:删除){
删除rootProject.buildDir
}

还有桌面模块的代码如下: build.gradle $ b $ pre $ apply plugin:'kotlin'

sourceSets.main.java.srcDirs = [ src /]
project.ext.mainClassName =io.wasin.testjavaapp.Main
project.ext.assetsDir = new File(../ assets);

dependencies {
testCompile group:'junit',name:'junit',version:'4.12'
compileorg.jetbrains.kotlin:kotlin-stdlib:$ kotlin_version

存储库{
mavenCentral()
}

任务运行(dependsOn:类,类型:JavaExec){
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
code>

不幸的是,它仍然没有正常工作,并且出现同样的错误。



Update 4



@aristotll在评论中提供了信息,面临同样的问题,但使用IntelliJ 。他说这可能是Android Studio的bug。所以我想,我会再尝试几次,然后决定它是否足够稳定,可以报告为错误。感谢您抽出宝贵时间进行测试。

解决方案

我也遇到了这个问题并解决了它。



在build.gradle中添加 runtimeClasspath文件(build / classes / kotlin / main)

 依赖关系{
//此依赖关系可在此组件和消费者的编译类路径中找到。
编译'com.google.guava:guava:23.0'

//使用JUnit测试框架
testCompile'junit:junit:4.12'
compileorg。 jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version
编译组:'dom4j',名称:'dom4j',版本:'1.6.1'
runtimeClasspath文件(build / classes / kotlin / main)

}



android-studio在build / calsses / kotlin / main中找不到类,添加runtimeClasspath。它可以工作!


I attempted to create a normal Kotlin project (non-android, just desktop/command line project) with Android Studio 2.3.3.

I followed the steps as laid out here but with different build.gradle content whose content I shared here.

Actually that content of build.gradle I copied from build.gradle file after generating a new project with IntelliJ Community Edition (2017.2).

Then I create a configuration to be able to run it on desktop with proper set up like Main Class, Working directory, and Use classpath of module. You can see in the image below.

and following is the project file structure I have

Then I sync the project, it succeeds. But error kicks in when I try to Run the project. I got the following error.

    "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=/Applications/Android Studio.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/charsets.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/localedata.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunec.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/jce.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/jsse.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/management-agent.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/resources.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/rt.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/dt.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/jconsole.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/sa-jdi.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/tools.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre7/1.1.4-2/272a21c30432c943d618008fbbd34762eb0d6c8a/kotlin-stdlib-jre7-1.1.4-2.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.1.4-2/fec983b2608a8a91a1b87dba18d247d692cf6b51/kotlin-stdlib-1.1.4-2.jar:/Users/haxpor/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre8/1.1.4-2/168cc3f45c307edda6c867dd0635a7d451257551/kotlin-stdlib-jre8-1.1.4-2.jar:/Applications/Android Studio.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain io.wasin.testjavaapp.Main
Exception in thread "main" java.lang.ClassNotFoundException: io.wasin.testjavaapp.Main
    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)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)

Process finished with exit code 1

I saw the classpath parameter as supplied in above /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java has no related directory to my project. I don't know how to add classpath directory via gradle despite I set up it in configurations already.

How can I solve this problem?

Update:

The following is my Main.kt code. It's just for testing purpose. So just printing something out.

package io.wasin.testjavaapp

object Main {
    @JvmStatic fun main(arg: Array<String>) {
        println("Hello world")
    }
}

Also I updated build.gradle for Kotlin version to match the version of Kotlin plugin on Android Studio. It's now ext.kotlin_version = '1.1.4-2'. Although still doesn't solve the problem.

Update 2

I checked inside build directory at the same level of src directory. There are no .class or intermediates files generated/built over there, only kotlin-build/caches/version.txt and that's it. No other files. So the problem might be due to the source files are not compiled at all?

Update 3

I tried creating a new module to see if it's the root cause or not. Also created assets folder just to act as Working Directory, rearranged source folder again to be as following.

We now have 2 modules.

  1. testJavaApp - root one
  2. desktop - consists of Main.kt and used as desktop executable

testJavaApp has the following code for build.gradle

group 'io.wasin'
version '1.0-SNAPSHOT'

// Top-level build file where you can add configuration options common to
all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.1.4-2'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

also desktop module has following code for build.gradle

apply plugin: 'kotlin'

sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "io.wasin.testjavaapp.Main"
project.ext.assetsDir = new File("../assets");

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

Unfortunately, it still didn't work and it's the same error that shown up.

Update 4

@aristotll provided info in a comment that he tested with Android Studio and faced the same problem, but not with IntelliJ. He said it might be Android Studio bug. So I guess, I would try a few more times then decide whether it's solid enough to report as a bug. Thanks for taking time to testing out.

解决方案

I also met this problem and solved it.

add runtimeClasspath files("build/classes/kotlin/main") in build.gradle

dependencies {
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:23.0'

// Use JUnit test framework
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
runtimeClasspath files("build/classes/kotlin/main")

}

because android-studio doesn't find class in build/calsses/kotlin/main, add runtimeClasspath .it works !

这篇关于Android Studio上的Kotlin(非Android)项目的ClassNotFoundException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:03