I have a Java project in IntelliJ that compiles, and now I am slowly changing.
Is there a way to ask IntelliJ to run the project, even if some parts of the code still do not compile? If so, how?
Why I would need this, you ask? see this ticket:
IDEA-61945 Run and Debug commands should ignore compile errors not related to the main being run.
http://youtrack.jetbrains.net/issue/IDEA-61945?query=it#tab=Comments
Please, do not answer this post questioning whether I should or I should not need to run a project even if it does not compile. please.
If not possible in IntelliJ, is it possible in maven? How?
In Intellij 12 you also have the following option (which personally I find the best one):
Go to Edit configuration of your launcher
Go to the before launch section
If 'Make' is in the list: remove it
Add 'Make, no error check'
Now, when you run, a make will still be done automatically but the run will continue even if the are compilation errors.
Regarding the above debate; I think it makes perfect sense to be able to run a part of the code that does compile even if another part of the code does not not; e.g. if that other part of the code belongs to a module that is in your project but not involved when running.
I ran into this exact same problem at work today. Before now, I probably would have been quick to jump on the bandwagon of, "Why would you ever want to do that?" Turns out that Eclipse lets you do exactly this, and if you start working concurrently with other developers who depend on this feature (which is to say, check in code that doesn't compile), it's handy to be able to do the same in IDEA!
And lucky for us IDEA users, you can. Follow these instructions from the FAQ for Eclipse Users, and you're good to go:
To be able to run code with errors, you can select the Eclipse
compiler in Settings dialog, Compiler, Java Compiler and add the
-proceedOnError option to the Additional command line parameters for the compiler.
The only thing that's lame is that it's not quite as seamless as in Eclipse. First, you'll have to untick the option to Make before run because IDEA won't run if make fails. Then, you'll have to remember to build before running. With those caveats, though, you should be able to accomplish what you're after.
Stijn Geukens's answer is correct, but it can be improved.
In Intellij Idea version 12 instead of removing the "Make" rule it can be replaced with "Make, no error check". This way project will be rebuilt (compiler will atempt to do it), but it will run the program independently of compile outcome.
If you want to debug just one part, then you can create a unit test around that. If you do not use the class that does not compile, then you can still debug the unit test related code.
When there are compilation errors, you can exclude specific files from compilation.
Go to the Messages window (if it is not visible: View -> Tool Windows
-> Messages)
Right click the problem file
Exclude from compile
At least in Intellij 12 you can achieve this.
First try to compile the project, including the broken class(es).
Then in the Messages view, containing all the compile errors:
Right-click the class you want to exclude
Click 'exclude from compile'
See this question on how to reinclude afterwards.
For Intellij 2017.3.1 my configuration is like this:
Use the Eclipse Compiler: Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Use compiler: Eclipse
Select "Proceed on errors"
Edit your desired configuration defaults (I use this for JUnit) before launch to Build, no error check: Check this screenshot
Additional step in order for Intellij not to open the classes with errors when you run your configuration. Un-select Automatically show first error in editor in Settings -> Build, Execution, Deployment -> Compiler
PS: This configuration is not perfect for all usages. It only works when you are fixing unit tests that were failing because of your changes in implementation code. When you go back to implementing features it is more useful to disable this feature since it will let you run you implementation code with errors and it will not jump to compilation errors. You need to go back and forth with changing the Eclipse compiler with the Javac one for best results.
Wow, it's been a while since I've been in IntelliJ and I miss it dearly! From my recollection you should be able to right click the main method in a module and run it directly so long as the remainder of the files in the module compile. I don't think it matters that a second module in the same project has errors. Is that not working for you?
updating for version 2017 - 2.5 community as menu options are slightly different
Navigate to:
Run>Edit Configurations
near the bottom of the Run/debug config window look for
"before launch: Activate tool window "
the field below this heading lists your current build config settings.
Use the + and - symbols in order to add and remove build preferences.
Once completed
Select apply then Okay
Thats it!
I don't think its possible at all. How you can run something that doesn't compile? That would be like driving a car that isn't put together. You could comment out the files that don't compile, so that the project compiles....
Edit -- or you can have Intellij not count the file as source by
Right Click on your project -> open module settings -> select your module -> select the file -> excluded
Related
How do I enable partial compiles in IntelliJ?
The same feature in NetBeans where by one can run a main method in a class without having non-dependecies in the same project be required to also compile.
EDIT:
After performing what CrazyCoder suggested, with some success, now I get ClassNotFoundException when trying to run a file not associated with those that were not compiling. After fixing the non-compiling files, it runs just fine.
I actually made in error in the same non-compiling file to test and it still works.
Could it be a bug?
EDIT:
Followed instructions still not working.
IntelliJ IDEA doesn't welcome working on the project that fails to compile, but there are several options:
use Compile action from the file/folder context menu, disable Build in Run/Debug configuration.
in the Before launch section of the Run/Debug configuration remove Build and add Build, no error check instead. Now start the configuration and it will ignore compilation errors trying to run on whatever classes managed to compile.
You should note that Make will fail on the first error and will not try to proceed further. In this case you should use explicit Compile action. Make also deletes output class files for the sources with errors.
If you want compilation to proceed after errors, you should switch to Eclipse compiler in IntelliJ IDEA Settings | Compiler | Java Compiler. When Eclipse compiler is selected, Proceed on errors option appears and it's enabled by default. With Eclipse compiler, an option to Proceed on errors enabled and Build, no error check in Before launch section you should get the desired behavior.
Navigate to the Build menu at the bottom of your test editor, right-click on the folder that contains error messages that you find irrelevant.
Choose "Exclude from compile".
To make CrazyCoder comment more visible, if you go for the "proceed on errors" behavior you may want to uncheck
Settings | Compiler | Automatically show first error in editor.
I'm working with 2020.2.3 version of IntelliJ.
I've tried all CrazyCoder's suggestions, but nothing works for me.
Eclipse Compiler + Build No-Error-Check
IntelliJ just refuses to Run anything (unit tests, main() methods...) because errors exist in the project.
All I can say is that Eclipse is still working where IntelliJ remains blocked.
Step 1: Setting as follow:
Step2: Build Project Automatically
Step 3: Modify Run configuration
Step 4: close all app console-view tabs in Run View, then run app again Ctrl+Shift+F10
Recompile only selected files:
Select packages or files that needs to be compiled.
Menu → Build → Recompile selected files (⇧ ⌘ F9)
If you need to run or debug, you need to set Do not build before run:
Menu → Run → Edit Configurations...
☑︎ Your target run configuration
Run / Modify options → Java / ☑︎ Do not build before run
Another way of doing this is to set
Use classpath for module field
in Junit configuration tab in your test configurations as your own module. By default it is all_local_extensions.
Attached screenshots.
I have a project named "Another one". It has 2 programs which do not have any relation with each other. The programs are "abc.java" and "def.java".
Both programs are for printing "Hello World".
If "abc.java" does not have any mistakes then only the compiler compiles "def.java",even though "def.java" does not have any mistakes.
Do I have to create one project for each program I make. I am new to programming.
def.java works fine here
when I removed a semicolon from abc.java, def.java stops working
Project structure
Module Structure
First, in IntelliJ you don't think in terms of "programs", but in terms of "project" and "modules". The most basic one is a project where the single module is located where the project is located (e.g. Project at D:\projects\MyProjects with 1 module at D:\projects\MyProjects).
What I can think of going wrong:
Your module containing abc.java is not actually a module. if you can execute abc.java with a run configuration, it should be a module.
Your build settings are somewhat wrong.
Compiler errors should keep you from executing the program to prevent misbehaviour in your application. For me, I can only not execute a module if the other modules have any compiler error while also being a dependency for the executing module. But not sure if a tool/plugin is causing this behaviour.
Also, you can check if you set up your modules correctly. Check the image above if your configuration looks similiar.
I hope I can help you. If not, maybe update your post providing some screenshots of your setup and configurations as I have shown you.
Get your definitions and terms right before doing anything else.
And then tell people what you're actually doing.
As stated, IntelliJ (and indeed all modern IDEs) have modules and projects. When you execute a target in a module, that entire module gets built IF NEEDED.
If "abc.java" does not have any mistakes then only the compiler compiles "def.java",even though "def.java" does not have any mistakes
This does not make any sense whatsoever. The compiler will compile whatever needs compiling. But most likely you're not talking about compiling, you're clicking the "run" button which will run whatever it is configured to run.
If you only have 1 run configuration set up, that's what will ALWAYS be run pressing that button.
If you want to run something without creating an explicit run configuration, just right click the open source file and select "Run".
This will cause ALL changed sources in the module to be compiled, no mumbo jumbo about it "compiling x despite y not having errors".
It will also attempt to compile everything that does have errors, and fail.
I have a large project with several modules including one large "src" module, and each compilation takes at least 5-10 minutes. I'm unable to refactor the structure of the project to potentially speed up compilation.
Every time I try to run a JUnit test, IntelliJ always compiles the entire module before running the tests (even if no files changed).
Other answers suggested using the Eclipse compiler and the "Make, no error check" launch command instead of the regular "Make". I tried that but IntelliJ is still rebuilding the entire module.
Edit: This seems to be related to how errors and handled with "Make, No Error Check". My project contains errors in unrelated areas of the code (managed by other teams) that I was using the eclipse compiler to skip over. After "fixing" those errors, incremental compilation works again. Maybe the build is considered invalid (and is discarded) even if errors are skipped?
Change your run configuration to not make the module:
Go to Run -> Edit Configurations (or click the Edit Configurations from the Run dropdown menu), and you'll see this screen:
Remove "Make" from Before launch, and it should work
P.S. I would suggest renaming it to something like: JUnit tests (NO REBUILD) otherwise you might be in for some serious head-scratching later on :)
I'm looking for an IntelliJ IDEA plugin that would run my tests each time I change my code. I looked for such a solution and I found:
Infinitest, which works, but is inconvenient because I need to add the facet to each module, and it opens a new tool window for each module (which means 15 tool windows for me).
Fireworks - didn't work for me, maybe it just doesn't work with IDEA 14 (in its repo I can see that last changes were made in 2009). IntelliJ also reports that it throws exceptions.
There are lots of ways I could run all my tests (including writing a simple script for this), but I'm looking for a tool that would be smart enough to rerun failed tests first, and that would understand module dependencies (so after a change in some module it would run only tests of dependent modules).
I prefer free options, but if there's something paid for a reasonable price, I would accept it as well.
IntelliJ now actually has a Toggle auto-testin the run dialog. Just run a run-configuration and then select Toggle auto-testin the run dialog.
It's not as intelligent as you would have hoped. It just reruns when it detects changes.
I know this is a 3-year-old question but I think it will help people who face the similar problem in future. So I found out a way to enable SBT style auto test execution in IntelliJ studio.
We need to do 2 things to enable auto test execution.
Enabling auto project build - This can be done in settings by going into File -> Settings -> Build, Execution... -> Compiler and selecting "Build Project automatically".
Enabling "Toggle auto-test" in run dialog box
This will start auto testing. Although this works fine, it takes time to build the project even when my project is tiny so for larger projects it will certainly take very long time to complete the build and execute tests.
Reference: Original article which explains these steps
If you are OK running tests which cover a single method chosen by you, you can try this plugin (it is continuos in the sense that you make changes to a method, then click on the method and the plugin will run all the unit tests automatically which cover that method): https://plugins.jetbrains.com/plugin/15063-unit-test-coverage-history-runner
You can use the Intellij Teamcity plugin. Teamcity is a paid product but there is a free version which gives you 20 projects and 3 agents for free .
It has a remote run feature using which you can send in unchecked code to run tests before committing.
It also has options to run failed tests first
Usage instructions for Remote Run
How do I enable partial compiles in IntelliJ?
The same feature in NetBeans where by one can run a main method in a class without having non-dependecies in the same project be required to also compile.
EDIT:
After performing what CrazyCoder suggested, with some success, now I get ClassNotFoundException when trying to run a file not associated with those that were not compiling. After fixing the non-compiling files, it runs just fine.
I actually made in error in the same non-compiling file to test and it still works.
Could it be a bug?
EDIT:
Followed instructions still not working.
IntelliJ IDEA doesn't welcome working on the project that fails to compile, but there are several options:
use Compile action from the file/folder context menu, disable Build in Run/Debug configuration.
in the Before launch section of the Run/Debug configuration remove Build and add Build, no error check instead. Now start the configuration and it will ignore compilation errors trying to run on whatever classes managed to compile.
You should note that Make will fail on the first error and will not try to proceed further. In this case you should use explicit Compile action. Make also deletes output class files for the sources with errors.
If you want compilation to proceed after errors, you should switch to Eclipse compiler in IntelliJ IDEA Settings | Compiler | Java Compiler. When Eclipse compiler is selected, Proceed on errors option appears and it's enabled by default. With Eclipse compiler, an option to Proceed on errors enabled and Build, no error check in Before launch section you should get the desired behavior.
Navigate to the Build menu at the bottom of your test editor, right-click on the folder that contains error messages that you find irrelevant.
Choose "Exclude from compile".
To make CrazyCoder comment more visible, if you go for the "proceed on errors" behavior you may want to uncheck
Settings | Compiler | Automatically show first error in editor.
I'm working with 2020.2.3 version of IntelliJ.
I've tried all CrazyCoder's suggestions, but nothing works for me.
Eclipse Compiler + Build No-Error-Check
IntelliJ just refuses to Run anything (unit tests, main() methods...) because errors exist in the project.
All I can say is that Eclipse is still working where IntelliJ remains blocked.
Step 1: Setting as follow:
Step2: Build Project Automatically
Step 3: Modify Run configuration
Step 4: close all app console-view tabs in Run View, then run app again Ctrl+Shift+F10
Recompile only selected files:
Select packages or files that needs to be compiled.
Menu → Build → Recompile selected files (⇧ ⌘ F9)
If you need to run or debug, you need to set Do not build before run:
Menu → Run → Edit Configurations...
☑︎ Your target run configuration
Run / Modify options → Java / ☑︎ Do not build before run
Another way of doing this is to set
Use classpath for module field
in Junit configuration tab in your test configurations as your own module. By default it is all_local_extensions.
Attached screenshots.