6

Is there a way to show line numbers in the Eclipse console?

My intent here is to show in the Eclipse console the file/line number that produced the output, not a serial line numbering of the messages (although that could also be an interesting question).

I am looking for something similar to Chrome's javascript console, where every line has a link that takes you to the function that printed it.

aioobe
  • 413,195
  • 112
  • 811
  • 826
sangil
  • 1,310
  • 2
  • 18
  • 25
  • 2
    If you're referring the stack trace, this has nothing to do with Eclipse, your code probably wasn't compiled with debug information. – Maroun May 25 '15 at 11:13
  • 1
    Do you mean line numbers of the output of your program, or line numbers in stack traces etc? – aioobe May 25 '15 at 11:13
  • 1
    not the stack trace. I mean the line numbers in the console window – sangil May 25 '15 at 11:36
  • There's no such feature in the Eclipse console. You can modify `System.out` to print a line number in front of each line. Let me know if you're interesting in such solution. – aioobe May 25 '15 at 11:38
  • @aioobe - how do you know for sure there is no such feature? – sangil May 25 '15 at 11:39
  • Because I've used Eclipse for about 10 years myself, and looking at the settings for the console view doesn't show anything. Googling for this (which has some pretty obvious search terms) doesn't show anything. That's why. – aioobe May 25 '15 at 11:41
  • @aioobe - fair enough. If there is no "better" answer in a few days, post your comment as an answer and I'll accept it. – sangil May 25 '15 at 11:42
  • How do you print into the console? – Aleksandr M May 25 '15 at 11:45
  • System.out.println(). But it's not relevant since my interest is runtime errors which are not explicitly printed by me – sangil May 25 '15 at 11:49
  • I don't print errors explicitly. I am referring to runtime errors such as uncaught exceptions which are printed by the system – sangil May 25 '15 at 11:50
  • Right click on a project -> Properties -> Java Compiler, check checkboxes under Classfile Generation. Clean, Re-build. – Aleksandr M May 25 '15 at 11:52
  • Look at my post, there is how to enable adding the line numbers information in the compilation process. – Krzysztof Cichocki May 25 '15 at 12:09
  • @AleksandrM- All these checkboxes were on, yet there are no line numbers in the console – sangil May 25 '15 at 13:07
  • @KrzysztofCichocki - I didn't find said post, can you post a direct link to it? – sangil May 25 '15 at 13:07
  • @sangil http://stackoverflow.com/a/30437119/4681060 – Krzysztof Cichocki May 26 '15 at 05:38
  • 1
    @sangil, it struck me, are you interested in the numbers of the lines in the output, i.e. `1: ...`, `2: ...`, `3: ...` or are you interested in the file/line that produced the output? – aioobe May 26 '15 at 07:16
  • @aioobe The file/line (i.e. option 2). I am looking for something similar to Chrome's developer tools, where the javascript console gives you a link (which is equivalent to line number AFAIC) to the line where the message was printed – sangil May 26 '15 at 14:36
  • Got it. I first thought you meant regular line numbers (option 1). The other variant is not available either, but can be solved using a custom System.out. See my updated answer. – aioobe May 26 '15 at 14:40

3 Answers3

8

You can create a custom PrintStream that inspects the current stack trace and includes file / line numbers in the output. You can then use System.setOut/System.setErr to cause all stdout/stderr output to include this information.

By formatting it properly Eclipse will pick it up as stack trace elements and generate links.

Here's a complete demo:

public class Main {
    public static void main(String args[]) {
        System.setOut(new java.io.PrintStream(System.out) {

            private StackTraceElement getCallSite() {
                for (StackTraceElement e : Thread.currentThread()
                        .getStackTrace())
                    if (!e.getMethodName().equals("getStackTrace")
                            && !e.getClassName().equals(getClass().getName()))
                        return e;
                return null;
            }

            @Override
            public void println(String s) {
                println((Object) s);
            }

            @Override
            public void println(Object o) {
                StackTraceElement e = getCallSite();
                String callSite = e == null ? "??" :
                    String.format("%s.%s(%s:%d)",
                                  e.getClassName(),
                                  e.getMethodName(),
                                  e.getFileName(),
                                  e.getLineNumber());
                super.println(o + "\t\tat " + callSite);
            }
        });

        System.out.println("Hello world");
        printStuff();
    }

    public static void printStuff() {
        System.out.println("More output!");
    }
}

Eclipse Console:

enter image description here

I consider this to be a hack though, and I wouldn't use it for anything but casual debugging.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I am getting an error on the first line: "The method setOut(PrintStream) in the type System is not applicable for the arguments (new PrintStream(){})" – sangil May 26 '15 at 14:50
  • I think you need `import java.io.PrintStream;`. – aioobe May 26 '15 at 14:51
  • [Here](http://ideone.com/rKG9YJ) is a complete example. (Note that it won't give you links in eclipse though.) – aioobe May 26 '15 at 14:53
  • @sangil, whoa, I managed to trick my Eclipse console to think this was part of a stack trace and it actually gives you links. Answer updated, try it out and see if it works for you too :-) – aioobe May 26 '15 at 15:01
  • Awesome, thumbs up :-) – sangil May 26 '15 at 15:07
0

You need to add line output to the exception handler.

For example:

...
        } catch (Exception e) {
            System.out.println(e.getStackTrace()[0].getLineNumber());
        }
...

This code will print out the line that caused the exception.

eggnukes
  • 180
  • 3
  • 20
  • My goal here was to find a way to make the console print line numbers for *everything*, just like line numbers are displayed in the editor window – sangil May 25 '15 at 12:59
  • @sangil In that case I think **aioobe** is onto something good here. However, you said that can't find the code that caused the error. I think a simple `e.printStackTrace();` in exception handler would be enough. – eggnukes May 25 '15 at 14:29
  • yes but which exception handler? I'm not looking for a localized fix but rather a way to make **all** line numbers display in the console. It very well may be that this is not possible... – sangil May 25 '15 at 14:36
0

At first I will rephrase your question in my own words so that you can check if I got your question right: You want to have all console output to be linked to the source code which produced it.

In this case this is NOT possible without using some modifying the standard libraries (System.out and System.err). Such a code would probably use the current stacktrace (see e.g. Is there a way to dump a stack trace without throwing an exception in java?) to determine the code (and line-number) where the print-statement was called. The next step would be to create an eclipse plugin which uses the gathered information to jump to the determined line (or to make eclipse think it is displaying a stacktrace).

Community
  • 1
  • 1
mschenk74
  • 3,561
  • 1
  • 21
  • 34
  • Basically you got it right. Linking is nice but not necessary, providing file/class/line number is enough. It would be great if no modification is necessary, but that's not mandatory. I would however would like to avoid creating plugins etc. See aioobe's answer above for an example – sangil May 26 '15 at 15:02