Harry Sharma
Harry Sharma

Reputation: 2200

Unable to run Scratch file in Android studio

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

Answers (2)

Andy Jazz
Andy Jazz

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 CommandOptionW to run the scratch file, and if you have a Windows machine, press the shortcut ControlAltW. 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

ekimai
ekimai

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:

  1. Right-click where you want to create the scratch file, e.g. Scratches and Consoles -> scratches and select New -> scratch file
  2. Select "Kotlin"
  3. Enter code, println("Hello World!")
  4. Right-click on the file name and select Run [scratch file].kts

This will create a run config for you to use later.

Run

When you run the hello world example: Result

Upvotes: 2

Related Questions