简体   繁体   English

Eclipse & Java:使 Eclipse 在运行时也编译代码

[英]Eclipse & Java: make Eclipse also compile the code when run

Note: I just started using Eclipse, so bear with me.注意:我刚开始使用 Eclipse,请耐心等待。

When I run a program in Eclipse, I see that it does not automatically compile the class file (using JDK 14);当我在 Eclipse 中运行程序时,我看到它不会自动编译 class 文件(使用 JDK 14); I have to manually run javac every time.我每次都必须手动运行javac I have "Build Automatically" checked as well.我也检查了“自动构建”。 How do I make Eclipse compile the class file automatically when run?如何让 Eclipse 在运行时自动编译 class 文件?

Eclipse does automatically compile the class file, every time you save any sourec file.每次保存任何源文件时, Eclipse都会自动编译 class 文件。

Some things to check:需要检查的一些事项:

  1. The java file has to be in a 'source folder'. java 文件必须位于“源文件夹”中。 Check this;检查这个; you can right click the project, select 'properties...' and look at the build path settings, which has a section for source paths.您可以右键单击项目 select 'properties...' 并查看构建路径设置,其中包含源路径部分。

  2. Each source path has an associated target dir.每个源路径都有一个关联的目标目录。 By default, this is named bin .默认情况下,它被命名为bin THAT is what where your auto-compiled-on-save class files are at.这就是您在保存时自动编译的class文件所在的位置。

  3. If all is set up properly, you can right click any java file with a 'main' method from eg the package explorer, select 'debug' from there, and see it in action.如果一切设置正确,您可以使用“主要”方法右键单击任何 java 文件,例如 package 资源管理器、select 并从那里调试。 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.如果这没有发生,则说明您没有正确配置项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM