Benjamin Confino
Benjamin Confino

Reputation: 2364

Can I use the eclipse debugger to write a variable to a file

Is it possible to create some sort of "trace breakpoint" in eclipse whereby Eclipse will log variables that I choose to a file when a "breakpoint" is hit without pausing the application?

Upvotes: 1

Views: 871

Answers (2)

weberjn
weberjn

Reputation: 1985

For Java 11 Michał Grzejszczak's answer can be written as java.nio.file.Files.writeString(java.nio.file.Path.of("f.txt"), "My string to save");return true;

(Best way to write String to file using java nio)

Upvotes: 1

Michał Grzejszczak
Michał Grzejszczak

Reputation: 2617

Sure. For this sample method:

public static void logArgs(final String one, final String two) {
    System.out.println(one + two);
}

put your breakpoint in it, right click it and edit Breakpoint properties... as in the example. The important checkboxes are Conditional and Suspend when 'true'. Just return false so that breakpoint does not suspend at all.

java.io.FileWriter w = new java.io.FileWriter("/tmp/file.txt", true);
w.write(String.format("args(%s, %s)%n"), one, two));
w.close();
return false;

enter image description here

Upvotes: 3

Related Questions