Make sure you include this option for javac when compiling your classes:
-g:lines,source,vars
The "-g" compiler option can be used to control how much debugging information should be generated into the class files (see documentation)
Here is a simple example with lambdas:
package test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestLambda {
public static Comparator<String> comparator1() {
return (o1, o2) -> {
return o1.compareTo(o2);
};
}
public static Comparator<String> comparator2() {
return (o1, o2) -> {
System.out.println("test");
if (true) {
throw new RuntimeException("Exception"); // line 20: stacktrace points to this line
}
return o1.compareTo(o2);
};
}
public static void main(String[] args) {
List<String> strings = Arrays.asList("string1", "string2", "string3");
Collections.sort(strings, comparator2());
}
}
Here is the stacktrace:
Exception in thread "main" java.lang.RuntimeException: Exception
at test.TestLambda.lambda$comparator2$1(TestLambda.java:20)
at test.TestLambda$$Lambda$1/189568618.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:216)
at java.util.Arrays.sort(Arrays.java:1438)
at java.util.Arrays$ArrayList.sort(Arrays.java:3895)
at java.util.Collections.sort(Collections.java:175)
at test.TestLambda.main(TestLambda.java:29)
As you can see the stacktrace at test.TestLambda.lambda$comparator2$1(TestLambda.java:20)
points to the exact line of the source code.
Your IDE should be able to parse the stacktrace and decorate it with links clicking on which should bring you to the exact line in your sources (at least that's what IntelliJ IDEA does).
If you compile with -g:none
the stacktrace will be different:
Exception in thread "main" java.lang.RuntimeException: Exception
at test.TestLambda.lambda$comparator2$1(Unknown Source)
at test.TestLambda$$Lambda$1/189568618.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:216)
at java.util.Arrays.sort(Arrays.java:1438)
at java.util.Arrays$ArrayList.sort(Arrays.java:3895)
at java.util.Collections.sort(Collections.java:175)
at test.TestLambda.main(Unknown Source)
Update:
Below is another example that is closer to the one provided in the question:
package test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestLambda {
public static Comparator<String> comparator1() {
return (o1, o2) -> {
return o1.compareTo(o2);
};
}
public static Comparator<String> comparator2() {
return (o1, o2) -> {
System.out.println("test");
if (true) {
throw new RuntimeException("Exception");
}
return o1.compareTo(o2);
};
}
public static void main(String[] args) {
List strings = Arrays.asList(1, 2, 3);
Collections.sort(strings, comparator2());
}
}
The only difference is that it uses raw type for List thus making possible using String
comparator for list of Integers
. The stacktrace indeed doesn't contain the line number since the exception happened during casting and not in our source code:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at test.TestLambda$$Lambda$1/189568618.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:216)
at java.util.Arrays.sort(Arrays.java:1438)
at java.util.Arrays$ArrayList.sort(Arrays.java:3895)
at java.util.Collections.sort(Collections.java:175)
at test.TestLambda.main(TestLambda.java:29)
A rule of thumb here is not to use raw types which in this case will make the debugging process easier (What is a raw type and why shouldn't we use it?). The compiler can help you here too: include this option for javac:
-Xlint:all
The compiler will warn you about raw types a lots of other things. Add another option:
-Werror
and the compiler will produce an error instead of warning (useful when using with CI servers to ensure high quality of the source code)