Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse console link, duplicate filenames in package structure, which is opened?

Tags:

java

eclipse

I have an application that has two classes called "Service" in two different places in the package structure. The logging outputs the file and line number like this, eg: (Service.java:102) This becomes a clickable link in the console output in Eclipse. Normally, these links are great because you can find exactly of where the output was printed from with a single click. But now I have two Service.java files, doing two entirely different things, and they're in a different place in the package structure. I can't rename either of them. When I click on the link, it takes me to the wrong java file, even when the correct java file is open in the editor.

I've searched around, but I can't find the answer. Is there a way to tell Eclipse which java file to consider first? Or a way to tell which package to look in first? Something, anything, to make these clickable links useful again?

like image 385
Mike Avatar asked Nov 01 '22 15:11

Mike


2 Answers

I guess your logger is configured like this to ouput a log like that (Service.java:102) :

(%F:%L)

%F : Used to output the file name where the logging request was issued.

%L : Used to output the line number from where the logging request was issued.

Try to used %l instead

%l : Used to output location information of the caller which generated the logging event.

EDIT

this solution does not seem to work well, it prints

com.x.y.z.MyClass.myMethod(MyClass.java:36)

=> the link is only on the classname, same issue.

But using the following pattern will work

(%C.java:%L)

It will print a full link like this :

(com.x.y.z.MyClass.java:36)
like image 162
ToYonos Avatar answered Nov 15 '22 04:11

ToYonos


i know three ways how to print a clickable class link in the console output.

First way:

Just print the class name inside the parentheses: System.out.println("(Service.java:42)");

This is simple method and will work if you are not using ambiguous class names. Since Eclipse console does not have informations required to decide which file should be opened, i guess it will open the first occurence.

Second way:

In your case. I would do it by using StackTraceElement to print the class name. That way:

StackTraceElement element = new StackTraceElement(
            "Service",                          // Class name
            "myFunnyMethodName",                // Method name
            Service.class.getName()+".java",    // Path to File
            2);                                 // line number


System.out.println(element);

Third way:

If you don't want to create StackTraceElement you can get it from you current thread.

Example:

System.out.println(Thread.currentThread().getStackTrace()[1]);

EDIT:

The other option is to try the Grep Console Plugin for Eclipse. You can define your own Expressions with Link, Color, Popup etc...

like image 21
dieter Avatar answered Nov 15 '22 03:11

dieter