ddinchev
ddinchev

Reputation: 34673

IntelliJ IDEA run/debug just one file

I have a JAVA project in IntelliJ IDEA that has multiple packages. But some files in my packages have their own main() method and can be run on their own.

However if I do a right click on given file and choose "Debug/Run File.main()" IntelliJ will try to build all the files in the package, no matter if they are included or not.

Is there any way to run just that one file?

Upvotes: 34

Views: 55967

Answers (6)

TriMix
TriMix

Reputation: 341

If you are using gradle, none of these tricks work. This is because gradle controls what gets built.

If you set up one project the whole project builds. Create submodules in order to isolate a set of files from each other.

To do this.. Right Click the base of the project and select New -> Module

Move your Java class file/s there. They will build and run independent of other files in your project or other submodules.

Upvotes: 0

Carl Lee
Carl Lee

Reputation: 832

Attention, I have a puzzling problem like this. I have a java project created by Eclipse, and now I open it with IdeaJ, the problem is If you just open the project directory and DID NOT IMPORT it as a new project, there is NO RUN OPTION on each file right click menu.

Only if you create new project from existing directory, that you can run a single java file in IdeaJ, or you will not do what you want.

I hope this tip will help you, 'cause it bothers me a lot.

Upvotes: 1

Steven Spungin
Steven Spungin

Reputation: 29071

Here's a Maven-centric solution.

The default Intellij behavior is to remake the entire project, and that can be really annoying. I find myself adding a quick test class to run often, and I always have to:

  1. Remove the Build Project setting from the auto generated run configuration. And this is after it tries to build everything.
  2. Shift-Meta F9 (or equivalent) to build just the class at hand
  3. Run the debug configuration.

enter image description here

This still won't build the module though. So, here is what I do now.

  • Remove the default build project setting from the default run configuration so you don't trigger it every time a new config is auto created.
  • Use Maven (or Gradle)
  • Set the run config to run the maven compile goal for the module

enter image description here

Now, everytime I debug, only the module compiles, and incrementally.

Upvotes: 1

Rainmaker
Rainmaker

Reputation: 173

I was looking for the same thing. Google let me to this topic first, so I'll post my solution here.

It is possible by replacing "Make" in the run configuration by a custom tool.

Remove "Make" and click the plus sign. Now select "Run external tool". Click the plus sign again to create a new custom tool.

Specify your command line Java compiler and set the output directory to the macro $OutputPath$.

Here is my configuration: Tool configuration

Now, specify this to be run before launch: External tool window

Works perfectly for my goal.

Upvotes: 1

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37746

To run a single file in IntelliJ IDEA:

Way 1:

Right-click somewhere in the editing area and select Run 'ClassName.main()'. Say you want to run HelloWorld, you should do the steps below:

  1. Right-click somewhere in the editing area
  2. select Run 'HelloWorld.main()' from context menu enter image description here

Way 2:

  1. Open the file in editor
  2. Ctrl+Shift+F10

Upvotes: 10

meistermeier
meistermeier

Reputation: 8262

You can remove the Make task in your run configuration. But you have to compile the single class manually before launch (Right click or Build -> Compile your class).

Or you could even try to compile the whole project if you need more than a single class. This might work if you have no dependencies to a broken class.

Upvotes: 27

Related Questions