Android Studio 无法找到或加载“Scratch"的主类;文件 [英] Android Studio could not find or load main class of "Scratch" file

查看:132
本文介绍了Android Studio 无法找到或加载“Scratch"的主类;文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:直到Android Studio 3.4.2,以下问题仍然存在.

Update: Below problem is remained until Android Studio 3.4.2.

我使用 Android Studio 3.3 并创建了一个 scratch 文件(File > new > Scratch File > Java).IDE在~/.AndroidStudio3.3/config/scratches中创建了scratch.java:

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) {

    }
}

但是当我运行该临时文件时(通过按类名或 main 方法附近的绿色按钮),我收到此错误:

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

我认为 IDE 不会生成 Scratch.class,所以 Java 找不到它.有没有办法解决这个问题并从 IDE 运行暂存文件(不使用 Terminal)?

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)?

推荐答案

更新

我刚刚注意到这个问题已在 Android Studio 3.5 中修复!

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

我让它工作的唯一方法(在 Android Studio 3.4.1 中)是这种非常脏的方法:

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

  1. 转到Run > Edit Configurations > Scratch
  2. Beforelaunch底部添加(+)Run External tool
  3. 在弹出的外部工具中按+并填写下面的Tool Settings

  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

名称:编译 Scratch

程序:javac

参数:$FileName$

工作目录:你的临时文件目录

再次在External Tools弹出窗口中按+并填写以下Tool Settings

Again in External Tools popup press + and fill in the following Tool Settings

名称:Run Scratch

程序:java

参数:$FileClass$

工作目录:你的临时文件目录

奇怪的是,这仅在您使用类/主声明旁边的绿色箭头时才有效.如果您使用菜单中的运行按钮,这也会失败.

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.

您仍然会在运行窗口的 Scratch 选项卡中看到 not found 错误.但是会弹出两个额外的选项卡(每个外部工具一个).在 Run Scratch 选项卡中,您将获得输出.

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.

这篇关于Android Studio 无法找到或加载“Scratch"的主类;文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆