Development issue/problem:

I am trying to familiarize myself with Kotlin to use it in my Android applications. So I want to try out a few simple examples first, just to familiarize myself with the syntax.

In my project for Android, I created a class called Main.kt that uses only the main method.

fun main(args: Array) {
println(Hello World); }

Android Studio shows me the hotplate icon to the left of the main method, and when I click on it, it shows me the following three options :

1) Start MyInk

2) Minecraft Debugger

3) Leading the coated Meinect

I decided on the first one, but she throws me…

Exception to the main thread of java.lang.ClassNotFoundException: com.vikalp.kotlin.MainKt
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)
in java.lang.classLoader.loadClass (ClassLoader.java: 357)
in java.lang.forName0 (Native Method)
in java.lang.forName (Class.java: 264)
in com.intellij.rt.execution.application.AppMainV2.main (AppMainV2.java: 107)

I’m stuck with this little problem. Let me know if any of you have encountered a similar problem and what the solution is.

How can I solve this problem?

Solution 1:

Update :

Approach 1:

You can now create a Kotlin file with the empty main() method and then run the code directly from the start icon on the left (from line 7) in the android studio editor (AS version: 3.5.3), like this

This internally creates a TestKt (filename) class with a PSVM method (and the required structure) to execute the code.

The demo configuration (automatically generated) for running this file is as follows

Run single kotlin class with main function in android studio –

Approach 2 (tested with scratch file after AS 3.6) :

  1. Select the project view in the project navigation bar.
  2. Create a Kotlin scratch file via New -> Scratch file -> Kotlin
    Run single kotlin class with main function in android studio – .
  3. Now paste your code and see the result in the right window.

Run single kotlin class with main function in android studio –

Android Studio (intellij) provides the REPL (Real Eval Print Loop) tool for writing and executing master code.

  1. open kotlin REPL as a tool -> kotlin -> kotlin REPL

Run single kotlin class with main function in android studio –

  1. Write down your code.

Run single kotlin class with main function in android studio –

  1. Press Command + Enter (on Mac) to execute your code (note the key combination on different platforms).

Write the code or import the class

Run single kotlin class with main function in android studio –

Tips :

  • Rebuilding the project after changing the source code
  • Use the arrow key to return to the story

Solution 2:

Home class {
companion object {
@JvmStatic fun main(args: Array) {
println(Hello!)
}
}

or
Just make a configuration with the main class as the MainKt.

Run single kotlin class with main function in android studio –

Solution 3:

You can create a new Java library module where you can run non-Android projects, see this answer for instructions. This is a Java related problem, but it should work with the basic functions of Kotlin as well. Edit: I can’t get it to work now.

You can also use IntelliJ IDEA, which is Java/Kotlin/etc. IDE instead of Android, the community edition is free and supports Kotlin.

If you just want to run very simple code, you can do it online here: https://try.kotlinlang.org/.

Solution 4:

As mentioned in the problem tracking file, a temporary solution is to add the following script to the build.gradle of the :

subProjects { subProject ->afterEvaluate {if (subProject.plugins.hasPlugin(kotlin) && subProject.plugins.hasPlugin(java library)) {subProject.kotlin.copyClassesToJavaOutput = echtesubProject.jar.duplicatesStrategy = DuplicatesStrategy.EXCLUDE}}}}

See: https://issuetracker.google.com/issues/68021152#comment12.

Solution no. 5:

In my Android studio, you just open the Kotlin file with your main function, then a green execution triangle appears to the left of the main function. Just click on it to start this file.

Solution no. 6:

Currently, this is simply not possible in Android Studio 3.0.

An error has already been registered on this topic: https://issuetracker.google.com/issues/68021152.

Solution no. 7:

I have encountered the same problem, and the solution is to run your code in the test class folder in the test folder, then right click on Run {your test class}.

That’s enough if you just want to play with Kotlin.

Solution no. 8:

Maybe this method works with the gradle-3.3, it worked for me when I rented it.

Solution no. 9:

Tested with Android Studio 3.1.3.

Note that this is an edited version of my other answer.

With this method, you can have Java/Kotlin and Android modules in the same project and be able to compile and run Java/Kotlin modules as standalone projects.

  1. Open your Android project in Android Studio. If you don’t have one, make one.
  2. Click File > New module. Select the Java library and click Next.
  3. Enter the name of the package, etc. and click Done. You should now see the Java module in your Android project.
  4. Paste your Java/Kotlin code into the Java module you just created.
  5. Click on the drop-down menu to the left of the Start button. Click on Change Configuration…
  6. In the new window, click on the plus sign at the top left of the window and select Requests
  7. A new application configuration should appear, enter data such as your main class and module class path.
  8. Press OK.
  9. Then we need to add the Kotlin plugin. Add the following code to your build.gradle project layer (lines to be added are marked with >>>) :

buildcript {
>> ext.kotlin_version = ‘1.2.51’
repositories {
google()
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.3’
>>> classpath org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version

}
}

  1. Add the following code to your build.gradle module layer (lines to be added are marked with >>>) :

Apply the plugin : java library
>> apply the plugin : kotlin

dependencies [
implementing fileTree(dir : ‘libs’, include : [‘*.jar’])
>>> implementing org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version
>>> runtimeClasspath files(compileKotlin.destinationDir)
}

When you click the Run Now button, your Java/Kotlin module should compile and run.

If you receive an error: You couldn’t find or load the main class…, just enter your main class (as in step 7), even if the field is already filled in. Click Apply and then click OK.

Solution no. 10:

It is now supported (V 3.2.1 ).

I just updated my android studio, created a new
project and then waited for all the builds to finish (if you think you’re going to update something, please accept).

I then created a new Kotlin file and added my code, right-clicked and selected Run, and that was it.

In the console I see the following

Hello World
process completed with output code 0

Good luck!

 how to run .kt file in android studiorun single kotlin file android studiokotlin error no main class specifiedrun kotlin classsourceset with name 'main' not foundhow to write kotlin code in android studiokotlin repl classpathkotlin repl onlinehow to build kotlin projectbuilding with kotlinkotlin konintellij koanskotlinc command not foundcheck kotlin version android studiohow to check kotlin version in intellijkotlin in windows 10kotlin compiler daemonandroid background thread exampleandroid executor exampleandroid update ui from thread handlerandroid schedule background taskandroid thread examplehow to run a method in background in javaandroid convert kotlin to java onlineadd kotlin to existing android projectandroid kotlin java interopandroid java kotlin interoperabilityhow kotlin is interoperability with javainstantiate kotlin class in javahow to run kotlin app in android studiokotlin main function in classkotlin repl in android studiorun kotlin from command linekotlin android studiohow to run kotlin program in intellij

You May Also Like

Steps To Start And Join Live Voice Chat On Telegram

To attract more users to its cloud-based instant messaging solution, Telegram announced…

4 Browsers that are Super Awesome

Facebook Twitter Pinterest Reddit Messenger WhatsApp Xbox One supports a wide range…

Eve Spectrum 4K 144Hz Gaming Monitor – Review and First Look

The Spectrum 4K 144Hz monitor is increasingly becoming a good option for…

Something went wrong while downloading your template [Fix]

How to correct an error when loading a model? 1. Retrieve a…