Please Help! Intellij is not running the class I try to run if there is an error in another class in the whole project?
已回答
Hi, I'm new to Java programming and so far I can say I liked using Intellij since it is an user friendly IDE with useful shortcuts. The problem is, I created two classes lets say one of them is FirstClass and the other one is SecondClass. Firstclass is running succesfully but after then I had a compiler error while working on SecondClass. While there is an error on SecondClass, I cannot run FirstClass because it automatically goes and keeps telling me there s an error. Isn't there a way to make Intellij run regardless the errors from other classes? Please help, I dont want to use another IDE just because of such situation.
请先登录再写评论。
Could you please share your project with us? You can upload an archive using the JetBrains Uploads service and write here an ID you receive.
In the screenshot I uploaded above there are two classed made by me. Actually both of them are working now but when I had this issue, SecondClass.java was not working because I made something which led a compiler error. Now in SecondClass.java I did not close a curly bracket just to make an error on purpose(to see IDE wont let me run other class which runs without any problem). My problem is why IntelliJ compiles Whole project and see if there is a mistake in other classes? I cannot run FirstClass.java without fixing the problem in SecondClass.java because compiler checks the whole project first to make sure if there is an error, not the class which is currently on the screen. Isn't there a setting to turn off this?
Please, check this article in our documentation regarding the Build action. Shortly, when you use this action, IntelliJIDEA compiles all classes. To compile a single file or class, you can use the Build | Recompile 'class name' action.
I just open an account to comment this post and say that I have the same problem, is really annoying to run a specific class and an error in OTHER class that has nothing to do with the one that I'm running pops up and make waste time. Theres a real solution for this or not?
First, I'd like to talk about Gradle, there are a few things to do in IDEA, the sync must be done first to ensure the classpaths/libraries loaded.
Gradle has plugins for ECJ(Eclipse Java Compiler) https://plugins.gradle.org/plugin/io.github.themrmilchmann.ecj which you can use to build the project Java source code as Eclipse does(ignore error usually), but I didn't verify it.
Then we can tell Gradle to ignore the failed task to continue the execution as https://docs.gradle.org/current/userguide/command_line_interface.html#sec:continue_build_on_failure described:
build.gradle
Second, IDEA does support Eclipse Compiler and compile plus run even broken classes. Some are described in https://www.jetbrains.com/help/idea/migrating-from-eclipse-to-intellij-idea.html#eclipse-compiler . We can switch IDEA to use ECJ(Eclipse Java Compiler) instead of default JDK Javac(it always exited at first compiler error) in File | Settings | Build, Execution, Deployment | Compiler | Java Compiler , then switch to ECJ, enable the option Proceed on errors , this way you can enable compile and run any classes that even broken in any project(Gradle project need a bit extra settings, I'll describe it in next section). However, must tell IDEA to ignore the build errors before the run. So when you edit the run configurations in this dialog:
We need to click Add before launch task and then remove the default behavior: 1. Build then add a new one: 1. Build, no error check, this way, it exactly works line Eclipse: compile all classes including the broken ones and run it.
Then finally, let's go back to Gradle project, IDEA provides a build system over Gradle project that lets you build classes using ECJ, to do this, click File | Settings | Build, Execution, Deployment | Build Tools | Gradle, then switch Build and Run | Build and run using: and Run tests using: to
IntelliJ IDEA.As there are so many steps to do, I've recorded a small movie about how to do this, hope this could help you:
IntelliJ IDEA with Gradle ECJ run broken class how to
I'm having the exact same problem. It seems like I always have to jump through a bunch of hoops with IntelliJ to get basic functionality to work. Eclipse just does what I want without fighting with me the whole time.
Please check my above answer works for you. If this is not your case, welcome to share more info about your project type, structure etc.
I didn't want to go through all those steps, so I just commented out the line that was causing the problem in my other class.
The fact remains, trying to execute one program should not result in all the other programs automatically getting compiled, and I tried turning off the automatic build feature, and it still tried to build other programs in the same directory, and when failing to do so, stopped. This shouldn't still be happening in the year 2023. I notice a lot of issues with IntelliJ persist for years. I Google a problem, and people were having it in 2015, or 2019, and I still encounter the same problem.
Nov/2024
Same just started, tried all: Checking project structure, editor configs, chatGPT. Wasted few hours on this. The only quick fix is to create a new project and copy all files into it and try again(I did it 3 times in 2 weeks). let me see if i come across a way around it.
-BRK
1. File > Invalidate Caches.. > enable Clear file system cache and Local History option > Invalidate and Restart
2. If step 1 is not helping, removing the .idea folder under the project folder.
Bond Han Its not working>Welp. Here is a quick video
https://www.youtube.com/watch?v=0DUBcwuUzsE
BRK
By default, the IDE builds the entire project before running your code. You can change the behavior in a few clicks like this:
Other compiler-related settings are located here:
Documentation: