How to get Eclipse Console to hyperlink text to source code files?

Viewed 16211

In Code: System.out.println("myPackage.MyClass");

In Eclipse Console: myPackage.MyClass.myMethod

I want to click on the output (myPackage.MyClass.myMethod) in Console and it directly shows the corresponding method, similar to what happens for exception stack traces. Any Idea?

7 Answers

Here's a simple wrapper method based on everyone else's answers that can be used anywhere to get a string formatted so that the Eclipse console will link to the line in any file where getSourceCodeLine() is called. I also discovered that printing to System.err in Eclipse will be shown in red.

public static String getSourceCodeLine() {
    // An index of 1 references the calling method
    StackTraceElement ste = new Throwable().getStackTrace()[1]; 
    return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")";
}

public static void main( String[] args )
    System.out.println("Here's a link to " + getSourceCodeLine());
    System.err.println("And a red link to " + getSourceCodeLine());
}

To add to the other answers, you can link to the specific class with this quirky format:

java.util..(List.java:100)
java.awt..(List.java:100)

Related