3

Okay, so I have a very large project in Java that I have been working on for years (a game, if you must know).

Unfortunately, a mysterious message has started being printed. No matter the lengths of my searching, I cannot pinpoint the location of the message.

I know that you can override the println method, and I was curious if it was possible to have some sort of debugging info attached to the message being printed? Like the line or file it was printed from perhaps? I tried searching Google for this, but it is simply too detailed of a problem that I cannot find an answer.

Even worse, the message is very ambiguous. It is simply "-1".

Atleast I know now to use a proper logger instead of simply println().

David Harris
  • 2,697
  • 15
  • 27
  • Is there any reason a grep wouldn't show it? My first thought would be to use some sort of mocking framework to intercept the function call and print a stacktrace. – AdamSpurgin Nov 14 '13 at 22:08
  • Hopefully your *very large project* is written in a modular way that allows for unit testing and mocking. Otherwise I have no idea how you should fix this other than to re-write it so that it *is* testable. Your question doesn't give enough information that allows it to be answered directly. – Hovercraft Full Of Eels Nov 14 '13 at 22:10
  • I assume you use an IDE? In eclipse for instance you can search through all of the projectspace for method calls or pure strings (like -1), I would to search for the string "-1", somewhere, someplace it must be defined, you can track it from there. Unless it comes from an external .jar, it gets harder to find it then. – arynaq Nov 14 '13 at 22:10
  • Thanks Adam, I will look into that. HovercraftFullOfEels, it's not a super large project, simply big enough that it hinders my ability to find it. arynaq Yes, I use Eclipse. I've tried that, but I don't have many more options to try, so I may have to continue with that. – David Harris Nov 14 '13 at 22:12
  • 4
    you can put a conditional breakpoint in the println(String x) method http://imgur.com/Z4zKTLy – Frederic Close Nov 14 '13 at 22:17
  • Given that the output is "-1", I'd not be surprised if it's not the String "-1" that's being output but rather an `int`, `short` or `byte`. Finding that can be tedious; the cleanest thing to do would probably to exchange all `println()` calls with a proper logger. You can search the complete project for those. – blalasaadri Nov 14 '13 at 22:19
  • try http://stackoverflow.com/questions/3228427/redirect-system-out-println – kajacx Nov 14 '13 at 22:21
  • @FredericClose that's the easiest solution; you should post it as an answer. – rob Nov 14 '13 at 22:23
  • @rob thanks, just did that – Frederic Close Nov 15 '13 at 08:53

3 Answers3

6

In such a case you can use a conditional breakpoint in your ide. This is a really useful feature of ide that's not always well known.

You set a breakpoint and you want it to suspend the application only under some condition, in this case the parameter of the println method being your 'mysterious ufindable string'

In the case of Intelllij IDEA this would look like this:

enter image description here

Frederic Close
  • 9,389
  • 6
  • 56
  • 67
2

You can do

 new Exception().printStackTrace()

or similar without throwing the created exception to get a full stack trace for a current execution point. And there is no need to throw it. You may event save them into list for later processing.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
-1

This looks like a message from ArrayIndexOutOfBoundsException. Add breakpoint on this type of exceptions. Try this:

String[] arr = new String[0];
try{
    System.out.println(arr[-1]);
}catch (Exception e){
    System.out.println(e.getMessage());
}
Mikhail
  • 4,175
  • 15
  • 31