eclipse IntelliJ 运行单个 Java 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41135213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:21:05  来源:igfitidea点击:

IntelliJ run single Java file

javaeclipseintellij-idea

提问by user619804

I am switching from Eclipse to IntelliJ, I'm trying to do something that's really simple to do in Eclipse. This is probably a simple question, but I have a project in IntelliJ with a bunch of small .java class files with main classes. These are just small practice programs I use for testing. I am trying to right click one of the java class files, then 'Run'. I'm expecting the class to run and see its output in the console - it looks like IntelliJ is instead trying to compile the entire project, including test classes.

我正在从 Eclipse 切换到 IntelliJ,我正在尝试在 Eclipse 中做一些非常简单的事情。这可能是一个简单的问题,但我在 IntelliJ 中有一个项目,其中包含一堆带有主类的小型 .java 类文件。这些只是我用于测试的小型练习程序。我正在尝试右键单击 Java 类文件之一,然后单击“运行”。我期待该类运行并在控制台中看到它的输出 - 看起来 IntelliJ 正在尝试编译整个项目,包括测试类。

How can I make IntelliJ just run the single class file without attempting to do a build of the entire project?

如何让 IntelliJ 只运行单个类文件而不尝试构建整个项目?

Also - In eclipse I am used to copying arguments to my main class into an input window when running a single java class. Is there a way to do this in IntelliJ.

另外 - 在 eclipse 中,我习惯于在运行单个 java 类时将主类的参数复制到输入窗口中。有没有办法在 IntelliJ 中做到这一点。

Thanks, I know I can do this from a terminal, but for my purposes it would be helpful to quickly run small java programs from an ide.

谢谢,我知道我可以从终端执行此操作,但就我而言,从 ide 快速运行小型 Java 程序会很有帮助。

采纳答案by A.K.Desai

You can change the Default settings based on your needs, to either automatically build after each change or manually. This can be achieved by clicking on File-> Other Settings-> Default Settings. This is the popup window.

您可以根据需要更改默认设置,在每次更改后自动构建或手动构建。这可以通过单击File-> Other Settings->来实现Default Settings。这是弹出窗口。

Default Preferences

默认首选项

To change the run configurations, you can follow below steps.

要更改运行配置,您可以按照以下步骤操作。

  1. Goto Runtab at the top Toobar and click on Edit Configurations.
  2. Click +icon and select Application. This opens a new popup window where you can add all your VM arguments & Program arguments similar to Eclipse.
  1. 转到Run顶部工具栏的选项卡,然后单击Edit Configurations
  2. 单击+图标并选择Application。这将打开一个新的弹出窗口,您可以在其中添加类似于 Eclipse 的所有 VM 参数和程序参数。

回答by DimaSan

Another option could be useful and quick for you. Just write your class with mainmethod you want to run, and then press a little green triangle either behind your mainmethodor behind your class name:

另一个选项可能对您有用且快速。只需使用main要运行的方法编写类,然后main方法后面类名称后面按一个绿色的小三角形:

enter image description here

在此处输入图片说明

After that you will be able to choose an option e.g., Run, Debugor Run with Coverage:

之后,您将能够选择一个选项,例如RunDebugRun with Coverage

enter image description here

在此处输入图片说明

In this case IDEA will just run your class without building the entire project.

在这种情况下,IDEA 将只运行您的类,而无需构建整个项目。

回答by Abdullah Khan

Right click your class in the left side bar. Hit Run. But your class must have a main method.

在左侧栏中右键单击您的班级。点击运行。但是你的类必须有一个 main 方法。

enter image description here

在此处输入图片说明

回答by Thanos

You can also use the not-so-new-now Scratch Files, they're pretty good for trying simple things out and they are saved and you can easily find them under recent files, or by name.

您还可以使用现在不太新的临时文件,它们非常适合尝试简单的事情,并且已保存,您可以在最近的文件下或按名称轻松找到它们。

https://blog.jetbrains.com/idea/2014/09/intellij-idea-14-eap-138-2210-brings-scratch-files-and-better-mercurial-integration/

https://blog.jetbrains.com/idea/2014/09/intellij-idea-14-eap-138-2210-brings-scratch-files-and-better-mercurial-integration/