Eclipse和Java:使Eclipse在运行时也对代码进行编译

huangapple go评论58阅读模式
英文:

Eclipse & Java: make Eclipse also compile the code when run

问题

注意:我刚开始使用Eclipse,所以请容忍一下。

当我在Eclipse中运行程序时,我发现它不会自动编译类文件(使用JDK 14);我每次都必须手动运行javac。我也已经勾选了“自动构建”。如何使Eclipse在运行时自动编译类文件?

英文:

Note: I just started using Eclipse, so bear with me.

When I run a program in Eclipse, I see that it does not automatically compile the class file (using JDK 14); I have to manually run javac every time. I have "Build Automatically" checked as well. How do I make Eclipse compile the class file automatically when run?

答案1

得分: 1

Eclipse会在您保存任何源文件时自动编译类文件。

一些需要检查的事项:

  1. Java文件必须位于“源文件夹”中。您可以右键单击项目,选择“属性...”,查看构建路径设置,其中有一个用于源路径的部分。

  2. 每个源路径都有一个关联的目标目录。默认情况下,这个目录名为bin。那就是您自动编译保存的类文件所在的位置。

  3. 如果一切都正确设置,您可以在包资源管理器中右键单击任何具有“main”方法的Java文件,从中选择“调试”,然后查看它的运行情况。然后,您对文件进行编辑并保存,它将自动更新。因此,编写类似于:

class Test {
    public static void main(String[] args) throws Exception {
        while (true) {
            Thread.sleep(1000L);
            System.out.println("Ping123");
        }
    }
}

运行它,然后编辑字符串文字,保存后,您会立即看到新的字符串出现,无需编译任何内容,无需重新启动任何内容。如果这种情况 没有 发生,您尚未正确配置该项目。

英文:

Eclipse does automatically compile the class file, every time you save any sourec file.

Some things to check:

  1. The java file has to be in a 'source folder'. Check this; you can right click the project, select 'properties...' and look at the build path settings, which has a section for source paths.

  2. Each source path has an associated target dir. By default, this is named bin. THAT is what where your auto-compiled-on-save class files are at.

  3. If all is set up properly, you can right click any java file with a 'main' method from e.g. the package explorer, select 'debug' from there, and see it in action. If you then make an edit to a file and save, it'll update automatically. Thus, write something like:

class Test {
    public static void main(String[] args) throws Exception {
        while (true) {
            Thread.sleep(1000L);
            System.out.println("Ping123");
        }
    }
}

run it, then edit the string literal, hit save, and you'll see the new string show up immediately, without compiling anything, without restarting anything. If that's NOT happening, you haven't properly configured the project.

huangapple
  • 本文由 发表于 2020年7月26日 02:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63091789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定