Android Studio could not find or load main class of "Scratch" file
Asked Answered
H

1

17

Update: Below problem is remained until Android Studio 3.4.2.


I use Android Studio 3.3 and created a scratch file (File > new > Scratch File > Java). The IDE created scratch.java in ~/.AndroidStudio3.3/config/scratches:

class Scratch {
    public static void main(String[] args) {

    }
}

But when I run that scratch file ( by pressing green button near to the class name or main method), I get this error:

Error: Could not find or load main class Scratch

I think that IDE does not make Scratch.class and so Java cannot find it. Is there a way for solving this problem and running scratch files from IDE (without using Terminal)?

Haematocryal answered 23/1, 2019 at 7:32 Comment(3)
@ankuranurag2 No it's not related. I have just tested the very same example in IntelliJ 2018.3.3, it works perfectly. It something specific to Android Studio.Prove
I still have the same issue in Android Studio 3.3.2Clementius
issuetracker.google.com/issues/109409701Skald
P
2

Update

I just noticed that this issue was fixed in Android Studio 3.5!


The only way I got this to work (in Android Studio 3.4.1) is this very dirty approach:

  1. Go to Run > Edit Configurations > Scratch
  2. In Before launch at the bottom add (+) Run External tool
  3. In External Tools popup press + and fill in the following Tool Settings

    Name: Compile Scratch

    Program: javac

    Arguments: $FileName$

    Working Directory: your scratch file directory

  4. Press OK
  5. Again in External Tools popup press + and fill in the following Tool Settings

    Name: Run Scratch

    Program: java

    Arguments: $FileClass$

    Working Directory: your scratch file directory

  6. Press OK and Apply in Run/Debug Configurations

Weirdly this works only if you use the green arrows beside the class/main declaration. If you use the Run button in the menu this also fails.

You will still get the not found error in the Scratch tab of the run window. But two additional tabs will pop up (one for each external tool). In the Run Scratch tab you will get your output.

I personally don't like this, But it's the best workaround I came up with.

Peraza answered 1/7, 2019 at 19:41 Comment(1)
Thank you. But this does not work by pressing "green buttons". Only works if I do: Tools > external tools > compile scrach then Tools > external tools > run scratch. In this situation I prefer using command line directly. +1 vote for your tries.Haematocryal

© 2022 - 2024 — McMap. All rights reserved.