Reputation: 2200
I am trying to run Scratch files in Android studio 3.4.1 and unable to see the output. I have created two files one with .kts extension and other with .java extension.
Below mentioned is the .kts file
class Check {
fun main(args: Array<String>) {
println(args[0]+"Hello, World!")
}
}
As per this question Kotlin scratch file output is missing in Android Studio we can run this file by writing print statement at top-level but still error remains the same. Below mentioned is the error:
"C:\Program Files\Android\Android Studio1\jre\bin\java.exe" "-javaagent:C:\Program Files\Android\Android Studio1\lib\idea_rt.jar=8940:C:\Program Files\Android\Android Studio1\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Android\Android Studio1\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Program Files\Android\Android Studio1\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Program Files\Android\Android Studio1\plugins\Kotlin\kotlinc\lib\kotlin-stdlib.jar;C:\Program Files\Android\Android Studio1\plugins\Kotlin\kotlinc\lib\kotlin-script-runtime.jar" org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -kotlin-home "C:\Program Files\Android\Android Studio1\plugins\Kotlin\kotlinc" -script C:/Users/ch-e01460.SPICEMONEY/.AndroidStudio3.4/config/scratches/scratch.kts
When i try to run this .Java file
class Scratch {
public static void main(String[] args) {
println("Hello, World!");
}
}
I get the below mentioned error
Error: Could not find or load main class Scratch Process finished with exit code 1
I have checked many other solutions available here but none of them is working for me. Requesting everyone here to enlighten the path so that it can help others too. Thanks in advance.
Upvotes: 3
Views: 2636
Reputation: 58113
If your old scatch file doesn't work for some reason, delete it and create a new one. I created a new scratch file in Android Studio Ladybug | 2024.2.1 Patch 3 and pasted your code there. Everything works fine. If you have an Android Studio on a Mac, then use the combination Command–Option–W to run the scratch file, and if you have a Windows machine, press the shortcut Control–Alt–W. Don't forget to pull out the Side Panel (where the output is displayed) if it's slid to the right.
class Check {
fun main(args: Array<String>) {
println(args[0]+"Hello, World!")
}
}
val check = Check()
check.main(arrayOf("I said: ", "I thought: "))
Upvotes: 0
Reputation: 93
You don't need all the main class code - scratches are basically scripts.
A basic scratch.kts file can be created and run:
Scratches and Consoles -> scratches
and select New -> scratch file
println("Hello World!")
Run [scratch file].kts
This will create a run config for you to use later.
When you run the hello world example:
Upvotes: 2