开发者

IntelliJ. Debug a program even if it does not compile

开发者 https://www.devze.com 2023-04-04 16:30 出处:网络
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?

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 a开发者_StackOverflow社区nd 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:

  1. Use the Eclipse Compiler: Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Use compiler: Eclipse
  2. Select "Proceed on errors"
  3. Edit your desired configuration defaults (I use this for JUnit) before launch to Build, no error check: Check this screenshot
  4. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消