Java IntelliJ IDEA 只运行/调试一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26467569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ IDEA run/debug just one file
提问by ddinchev
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.
我在 IntelliJ IDEA 中有一个 JAVA 项目,它有多个包。但是我的包中的一些文件有自己的main()
方法,可以自己运行。
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.
但是,如果我右键单击给定的文件并选择“调试/运行 File.main()”,IntelliJ 将尝试构建包中的所有文件,无论它们是否包含在内。
Is there any way to run just that one file?
有没有办法只运行那个文件?
采纳答案by meistermeier
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).
您可以在运行配置中删除 Make 任务。但是您必须在启动之前手动编译单个类(右键单击或 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.
或者,如果您需要多个类,您甚至可以尝试编译整个项目。如果您对损坏的类没有依赖关系,这可能会起作用。
回答by Ripon Al Wasim
To run a single file in IntelliJ IDEA:
在 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:
右键单击编辑区域中的某处,然后选择 Run 'ClassName.main()'。假设您要运行 HelloWorld,您应该执行以下步骤:
Way 2:
方式二:
- Open the file in editor
- Ctrl+Shift+F10
- 在编辑器中打开文件
- Ctrl+Shift+F10
回答by Rainmaker
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.
可以通过自定义工具替换运行配置中的“Make”。
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$.
指定您的命令行 Java 编译器并将输出目录设置为宏 $OutputPath$。
Now, specify this to be run before launch:
Works perfectly for my goal.
非常适合我的目标。
回答by Steven Spungin
Here's a Maven-centric solution.
这是一个以 Maven 为中心的解决方案。
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:
默认的 Intellij 行为是重新制作整个项目,这真的很烦人。我发现自己添加了一个快速测试类来经常运行,而且我总是必须:
- Remove the Build Project setting from the auto generated run configuration. And this is after it tries to build everything.
- Shift-Meta F9 (or equivalent) to build just the class at hand
- Run the debug configuration.
- 从自动生成的运行配置中删除 Build Project 设置。这是在它尝试构建所有内容之后。
- Shift-Meta F9(或等效的)来构建手头的类
- 运行调试配置。
This still won't build the modulethough. So, here is what I do now.
尽管如此,这仍然不会构建模块。所以,这就是我现在要做的。
- Remove the default build projectsetting 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
- 从默认运行配置中删除默认构建项目设置,这样您就不会在每次自动创建新配置时触发它。
- 使用 Maven(或 Gradle)
- 设置运行配置以运行模块的 maven 编译目标
Now, everytime I debug, only the module compiles, and incrementally.
现在,每次我调试时,只有模块会编译,并且是增量式的。