I often run Java programs like Android apps in my Emacs compilation buffer while I’m working on them, so I see Java stack traces there just like compile errors. Emacs is good at identifying compilation errors so you can jump straight to the source file and line, but not so much with stack traces.
Here’s an elisp snippet that adds stack trace support, so you can jump to
individual lines with C-x `
, mouse click, or next-error
:
(defvar java-stack-trace-dir "src/")
(defun java-stack-trace-regexp-to-filename ()
"Generates a relative filename from java-stack-trace regexp match data."
(concat java-stack-trace-dir
(replace-regexp-in-string "\\." "/" (match-string 1))
(match-string 2)))
(add-to-list 'compilation-error-regexp-alist 'java-stack-trace)
(add-to-list 'compilation-error-regexp-alist-alist
'(java-stack-trace .
("^[[:space:]]*at \\(\\(?:[[:lower:]]+\\.\\)+\\)[^(]+(\\([[:alnum:]]+\\.java\\):\\([[:digit:]]+\\))"
java-stack-trace-regexp-to-filename 3)))
You can customize java-stack-trace-dir
to set the base directory to search for
source files. If it’s relative, it will be appended to the compilation’s working
directory.
Happy hacking!
Thanks for the excellent information!
I ran into a few issues with this implementation:
I modified the above implementation to resolve both:
https://github.com/coreyoconnor/RCs/blob/master/real-emacs.el#L175
I’m an emacs noob so there is rampant copy pasta in that file.
The regex that worked for me for just package sources:
(add-to-list ‘compilation-error-regexp-alist-alist
‘(java-stack-trace .
(“at \\(\\(?:[[:alnum:]]+\\.\\)+\\)+[[:alnum:]]+\\.[[:alnum:]]+(\\([[:alnum:]]+\\.java\\):\\([[:digit:]]+\\))$”
java-stack-trace-regexp-to-filename 3)))
thanks for the contribution, corey!