java 在 IntelliJ IDEA 中测试小代码片段的快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5682618/
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
Shortcuts for testing out small code snippets in IntelliJ IDEA?
提问by Nick
I use IntelliJ IDEA and was thinking about how nice it would be to be able to pop up a context where I could write, compile and run some simple core java code and then automatically dispose of the whole thing when I close the context.
我使用 IntelliJ IDEA 并且正在考虑能够弹出一个上下文,我可以在其中编写、编译和运行一些简单的核心 Java 代码,然后在我关闭上下文时自动处理整个内容。
Sometimes you just want to try simple something out, like a regular expression (I'm aware there's a regex tester plugin) or some series of bit-wise operations; for whatever reason its not always feasible to test directly within the project code and creating a whole new project is disruptive to the work flow.
有时你只想尝试一些简单的东西,比如正则表达式(我知道有一个正则表达式测试器插件)或一些系列的按位操作;无论出于何种原因,直接在项目代码中进行测试并不总是可行的,并且创建一个全新的项目会破坏工作流程。
So my question is this: what do other people use to try out their snippets of code?
所以我的问题是:其他人用什么来尝试他们的代码片段?
回答by Felix
回答by QuantumMechanic
I have a "CodePlay" project that exists completely locally (i.e. the sources are local and not in the SCM sandbox) and just add standalone classes to it when I need to play around with some simple code.
我有一个完全存在于本地的“CodePlay”项目(即源是本地的,而不是在 SCM 沙箱中),当我需要使用一些简单的代码时,只需向其中添加独立的类。
I understand this isn't exactly what you wanted ("dispose of the whole thing when I close the context") but since IntelliJ will let you open a project in a new window I don't find it to be disruptive of my main work since the "CodePlay" project window can be minimized or closed without affecting the "real" project window.
我知道这不是你想要的(“当我关闭上下文时处理整个事情”)但是由于 IntelliJ 会让你在新窗口中打开一个项目,我认为它不会破坏我的主要工作因为“CodePlay”项目窗口可以最小化或关闭而不影响“真实”项目窗口。
Yes, there's the overhead of setting it up once, but once the project is set up it loads pretty quickly because there's not much in it and what is in it is pretty short.
是的,设置一次会产生开销,但是一旦项目设置好,它的加载速度就会非常快,因为其中内容不多,而且内容很短。
回答by ROMANIA_engineer
Java 9
爪哇 9
You can use jshellfrom any command line (or from the IntelliJ terminal).
您可以从任何命令行(或从IntelliJ 终端)使用jshell。
View> Tool Windows> Terminal(Alt+ F12)
Type
jshell
:user@xyz:~/IdeaProjects/prj$ jshell | Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell>
Enter your snippet (you don't need to add
;
at the end of the row)jshell> Set.of("c", "b", "a").size() ==> 3
Exit from
jshell
using one of the following approaches:- type
/exit
- press Ctrl+ D
- type
视图>工具窗口>终端( Alt+ F12)
类型
jshell
:user@xyz:~/IdeaProjects/prj$ jshell | Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell>
输入您的代码段(您不需要
;
在行尾添加)jshell> Set.of("c", "b", "a").size() ==> 3
退出
jshell
使用以下方法之一:- 类型
/exit
- 按Ctrl+D
- 类型
回答by Dan Midwood
My method to test code is using JUnit and a temporary class. In the class I will create a single zero arg method with a JUnit @Test annotation. The code I am playing with will be inside the method, and then a simple key stroke will run that code.
我测试代码的方法是使用 JUnit 和一个临时类。在该类中,我将创建一个带有 JUnit @Test 注释的零 arg 方法。我正在使用的代码将在方法内部,然后一个简单的击键将运行该代码。
回答by Slav
I would also recommend compileonline.comwhich can be great for testing small snippets
我还推荐compileonline.com,它非常适合测试小片段
回答by Duane5000
The answer can be found here. Groovy shell (in IDEA) is a command-line application that lets you evaluate Groovy expressions, functions, define classes and run Groovy commands. The Groovy shell can be launched in Groovy projects and in Grails applications.
答案可以在这里找到。Groovy shell(在 IDEA 中)是一个命令行应用程序,可让您评估 Groovy 表达式、函数、定义类和运行 Groovy 命令。Groovy shell 可以在 Groovy 项目和 Grails 应用程序中启动。
StopCtrl+F2 Click this button to stop the current process.
停止Ctrl+F2 单击此按钮可停止当前进程。
CloseCtrl+Shift+F4 Click this button to close the selected tab of the Run tool window and terminate the current process.
关闭Ctrl+Shift+F4 单击此按钮可关闭运行工具窗口的选定选项卡并终止当前进程。
Execute Groovy Code(Ctrl+Enter) Run code, entered in the console.
执行 Groovy 代码(Ctrl+Enter) 运行代码,在控制台中输入。
HelpF1 Use this icon or shortcut to open the corresponding help page.
帮助F1 使用此图标或快捷方式打开相应的帮助页面。
and I'll be trying this myself
我会自己尝试这个