0
package com.example.manish.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

fun main(arg : Array<String>){
    print("HI")
}

LogCat:

Exception in thread "main" java.lang.ClassNotFoundException: com.example.manish.myapplication.MainActivityKt
    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.AppMainV2.main(AppMainV2.java:107)
3
  • And what is your question? Commented Sep 24, 2017 at 23:02
  • 1
    This might be a duplicate of stackoverflow.com/questions/44823064/… Commented Sep 25, 2017 at 0:41
  • Problem is seems same but still exist Commented Sep 25, 2017 at 7:08

2 Answers 2

2

This is likely a known issue in Android Studio and Kotlin inter-operation.

When you try to run a Kotlin class from a non-Android (e.g. pure Java + Kotlin) module, it does not add the separate directory, into which the Kotlin classes are compiled, to the classpath.

The workaround is to add these lines to your module's build.gradle:

dependencies {
    runtimeClasspath files(compileKotlin.destinationDir)
}

This will work for the main source set. To do the same for tests, use testRuntimeClasspath and compileTestKotlin respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

After doing this I am getting Error:(36, 0) Could not get unknown property 'compileKotlin' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I am getting error: Could not get unknown property 'destinationDir' for task ':compileKotlin' of type org.jetbrains.kotlin.gradle.tasks.KotlinCompile.
0

Is this an Android applicaton or a regular application? We can’t have a main function in an Android application; it has to be a regular desktop project.

But I dont why it is :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.