You can try that:
System.out.println("I just hit file@line: "+Thread.currentThread().getStackTrace()[1].getFileName() +'@'+ Thread.currentThread().getStackTrace()[1].getLineNumber());
Thread.currentThread().getStackTrace()
will return an array of StackTraceElement
, and we take the second element (the first one will be in the Thread
class), and get the file name and line number from that.
EDIT: You may also want to create a static function (you can put it in an utilitary class) to do that (which will avoid the problems with the different java implementations):
public static void printCurrentFileAndLine() {
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for (int i=0; i<ste.length; i++)
if (ste[i].getMethodName().equals("printCurrentFileAndLine")) {
System.out.println("I just hit file@line: "+ste[i+1].getFileName() +'@'+ ste[i+1].getLineNumber() );
break;
}
}
And you just need to call:
printCurrentFileAndLine();