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)
Manish Singla
  • 1,361
  • 1
  • 10
  • 15

2 Answers2

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.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 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. – Manish Singla Sep 25 '17 at 05:50
  • I am getting error: `Could not get unknown property 'destinationDir' for task ':compileKotlin' of type org.jetbrains.kotlin.gradle.tasks.KotlinCompile.` – Abhishek Saxena Dec 17 '22 at 10:34
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 :)

Manish Singla
  • 1,361
  • 1
  • 10
  • 15