Reputation: 34673
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
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
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
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:
This still won't build the module though. So, here is what I do now.
Now, everytime I debug, only the module compiles, and incrementally.
Upvotes: 1
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$.
Now, specify this to be run before launch:
Works perfectly for my goal.
Upvotes: 1
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:
Way 2:
Upvotes: 10
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