Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcuts for testing out small code snippets in IntelliJ IDEA?

Tags:

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.

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?

like image 926
Nick Avatar asked Apr 15 '11 21:04

Nick


People also ask

How do I run code snippet in IntelliJ?

We can type a short sequence, like psvm , and have IntelliJ IDEA generate things like the public static void main method. We can even create our own live templates for code snippets we commonly use. If we put the cursor inside the main method, we can run it by using ⌃⇧R, or Ctrl+Shift+F10 on Windows.

How do I test IntelliJ code?

In IntelliJ IDEA, you can jump between test classes and production code. In the editor, place the caret at the test class or at the test subject in the source code and press Ctrl+Shift+T (Navigate | Test Subject or Navigate | Test).


2 Answers

Take into account IntelliJ Idea's (from version 14) Scratches:

  • Press Ctrl+Shift+Alt+Insert and select type

enter image description here

like image 175
Felix Avatar answered Sep 20 '22 10:09

Felix


Java 9

You can use jshell from any command line (or from the IntelliJ terminal).

  1. View > Tool Windows > Terminal (Alt + F12)

  2. Type jshell:

    user@xyz:~/IdeaProjects/prj$ jshell
    |  Welcome to JShell -- Version 9-ea
    |  For an introduction type: /help intro
    
    
    jshell> 
    
  3. Enter your snippet (you don't need to add ; at the end of the row)

    jshell> Set.of("c", "b", "a").size()
    $1 ==> 3
    
  4. Exit from jshell using one of the following approaches:

    • type /exit
    • press Ctrl + D
like image 9
ROMANIA_engineer Avatar answered Sep 21 '22 10:09

ROMANIA_engineer