Unable to run scratch file

I’m trying to run a scratch file to no avail. When I run even a simple function:

println("Hello World")

I get this exception:

Exception  in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 1 more

This is with 1.2.60

Hello, I can’t reproduce this on Kotlin 1.2.60, IDEA 2018.2. Can you share the output of the “run” tool window when you invoke “Main menu → Run → Run ‘scratch.kts’”. For example:

/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/bin/java “-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=56071:/Applications/IntelliJ IDEA CE.app/Contents/bin” -Dfile.encoding=UTF-8 -classpath /Users/belkov/Idea/2018.2-CE/plugins/Kotlin/kotlinc/lib/kotlin-compiler.jar:/Users/belkov/Idea/2018.2-CE/plugins/Kotlin/kotlinc/lib/kotlin-reflect.jar:/Users/belkov/Idea/2018.2-CE/plugins/Kotlin/kotlinc/lib/kotlin-stdlib.jar:/Users/belkov/Idea/2018.2-CE/plugins/Kotlin/kotlinc/lib/kotlin-script-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -kotlin-home /Users/belkov/Idea/2018.2-CE/plugins/Kotlin/kotlinc -script /Users/belkov/Idea/2018.2-CE/config/scratches/scratch.kts
Hello World

Process finished with exit code 0

I am also getting this error when I attempt to run Kotlin scratch file with the Play button in the editor window’s toolbar (also Cmd+Alt+W on Mac).

When I run it as Alexey instructed, it does work.

Check out this search: https://youtrack.jetbrains.com/issues/KT?q=Subsystems:%20{IDE.%20Scratch}%20in%20thread%20"main"%20java.lang.NoClassDefFoundError

This is probably one of two problems:

  1. You’re running the scratch from Android Studio: https://youtrack.jetbrains.com/issue/KT-25450
  2. You’re trying to use project classes not in the scratch dependencies: https://youtrack.jetbrains.com/issue/KT-23523
1 Like

I’m experiencing the same issue (works from the main menu but not from the scratch run button) and don’t have any of those problems. Using the last version of community edition and not using any custom classes (the content of the scratch file is exactly this: println("hello world!")).