Search code examples
javatestingprojectexecute

Why does Java execute all files in folder, including closed ones?


I am writing Java code. I was using the if conditions to read the input. When I tried to test the code, it executes all the files in the folder. I am also using IntelliJ idea.

It executes my current file and saved java files.


Solution

  • Somehow I believe you have a project in your IDE, and you want to execute a (java) file. The file/class you think about has a public void main(String[]) method, yet instead of this file some other file gets executed?

    Very likely the first file you created with a main method was registered as the entry point in your IDE or project setup, and that is remembered when you trigger the run project action.

    Instead you need to find the correct keystroke or menu item to run file to get the focused file to execute.

    If, however you feel like the file is executed since compilation errors show up - well that is because all files get compiled in the background. You need to reconfigure the IDE to stop it doing that - but why would you want to? It does not harm if the compiler works in the background.