33

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?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
ddinchev
  • 33,683
  • 28
  • 88
  • 133
  • Rainmaker's answer is good. With the external tool, the one file is compiled automatically before it is run/debugged. But with meistermeier's answer, two steps are needed: compile the file manually, and then run it. – Yumin Jan 03 '20 at 03:23
  • If you have a "Name.test" module imported from Gradle or Maven, make sure your "Name.main" module is removed from its dependencies so it doesn't try to compile that. – ABadHaiku May 26 '21 at 02:51

6 Answers6

26

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.

meistermeier
  • 7,942
  • 2
  • 36
  • 45
  • Worked like charm, I need to wait another minute before accepting the answer! – ddinchev Oct 20 '14 at 14:15
  • How to do run configuration in IntelliJ IDEA? – Ripon Al Wasim Oct 27 '15 at 07:11
  • 1
    You just have to open your run config settings (the dropdown in your toolbar or shift + alt + F10 -> Edit configurations). Select the configuration you want to run and remove the make entry at the bottom (before launch section). If you never ran your project / target class than right click on the executable class and select 'Create '. – meistermeier Oct 27 '15 at 07:23
  • 1
    I didn't see any 'Make' task in my run configuration. Instead it had 'Build', removing which I could run main() inside each file. – displayName Apr 04 '19 at 02:41
  • For my IntelliJ it was the 'Build' option that was listed under "before launch" section. Having removed that, the individual class could then be run, without any conflicts. – Animesh Dec 14 '19 at 14:56
  • Recompiling manually each time I want to run the class is bit annoying.But, it works. – hermit Sep 01 '20 at 00:47
  • I am using the below version and cannot run a single java file, this file is in a maven project. (new to intellij used eclipse all my professional life). IntelliJ IDEA 2021.1.2 (Community Edition) Build #IC-211.7442.40, built on June 1, 2021 Runtime version: 11.0.11+9-b1341.57 amd64 VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o. Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1008M Cores: 8 Kotlin: 211-1.4.32-release-IJ7442.2 – Sandeep Jun 30 '21 at 08:22
10

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
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
  • This is a very convenient solution. +1 – qwr May 06 '16 at 04:58
  • 18
    Except that the author of the question specifically said he did not want all files to be built, and this suggestion builds everything (unless you have previously removed the "make" command from the run configuration as per meistermeier's response) . – user467257 May 08 '16 at 08:46
1

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.

Rainmaker
  • 173
  • 3
  • 13
1

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.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
1

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.

Carl Lee
  • 732
  • 5
  • 5
0

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.

TriMix
  • 231
  • 3
  • 4