How to print something to the console on breakpoint?
Asked Answered
S

1

6

I'm wondering if there is any option to print something to console on breakpoint. I had an idea to hack conditional breakpoints feature in IntelliJ IDEA.

I wrote such a class:

public class BreakpointPrinter {

    public static boolean print(Object object){
        System.out.println(object);
        return false;
    }
}

and use it like that: breakpoint hack

but unfortunately I get an error: breakpoint error

Do you have any better ideas to achieve such a goal?

Sphalerite answered 24/8, 2017 at 8:16 Comment(0)
L
7

Right click on the breakpoint:

enter image description here

Click on More

enter image description here

Select Evaluate and log and enter the code you want to execute.

In the above example the breakpoint executes System.out.println("I reached my breakpoint") and as soon as the breakpoint is reached that string is written to the IntelliJ console.

I suspect the reason you are getting a ClassNotFoundException is that your class (BreakpointPrinter) is not on the classpath for the JVM instance spawned by IntelliJ. However, if all you want to do is write to console everytime a specific breakpoint is reached then you can do so using a class which is always available in the JVM (e.g. java.lang.System) and you can trigger it via the breakpoint's Evaluate and log feature.

Leonidaleonidas answered 24/8, 2017 at 8:30 Comment(1)
Thanks! Helped me a lot. What I really wanted to do was printing something without stopping on breakpoint. This can be achieved by using your approach + unchecking "Suspend" checkbox on breakpointSphalerite

© 2022 - 2024 — McMap. All rights reserved.