eclipse-jdt / eclipse-jdt/eclipse.jdt.core
ECJ "-8" option may need better documentation
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
### Environment:
ECJ Version:
```
Eclipse Compiler for Java(TM) v20241112-0530, 3.40.0, Copyright IBM Corp 2000, 2020. All rights reserved.
```
Java Version:
```
openjdk version "1.8.0_442"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_442-b06)
OpenJDK 64-Bit Server VM (Temurin)(build 25.442-b06, mixed mode)
```
Javac Version:
```
javac 1.8.0_442
```
### Description:
The following test program invokes `java.lang.StackWalker` to print the stack trace of the current thread:
```java
public class Test {
public static void main(String[] strArr) {
java.lang.StackWalker.getInstance().forEach(frame ->
System.out.println(frame.getClassName() + " -> " + frame.getMethodName())
);
}
}
```
`java.lang.StackWalker` was introduced in **Java 9**, meaning it **should not be available** in Java 8.
However, when compiling this program using **ECJ** with Java 8 mode, **ECJ does not report any errors** and successfully generates a `.class` file.
To verify the compiled output, we inspected the generated `.class` file using `javap`:
```
Last modified Mar 12, 2025; size 1543 bytes
MD5 checksum c8412a812abcf9bc443d412660dea682
Compiled from "Test.java"
public class Test
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Class #2 // Test
```
Despite successful compilation, **executing the program leads to a runtime exception** due to the missing `java.lang.StackWalker` class in Java 8:
```
Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/StackWalker
at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: java.lang.StackWalker
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 1 more
```
By contrast, when compiling the same program with **`javac`**, it correctly reports a **compilation error**, preventing the invalid class from being generated:
```
Test.java:5: error: cannot find symbol
java.lang.StackWalker.getInstance().forEach(frame ->
^
symbol: class StackWalker
location: package java.lang
1 error
```
This indicates that ECJ **incorrectly allows the usage of Java 9+ APIs** when compiling with Java 8 target settings, leading to a misleading successful compilation and eventual runtime failure.
### Steps to Reproduce:
1. **Compile the test program using ECJ:**
```
java -jar ecj-4.34.jar -8 -d . Test.java
```
- Expected behavior: Compilation should fail due to `java.lang.StackWalker` being unavailable in Java 8.
- Actual behavior: Compilation succeeds, generating a `.class` file.
2. **Compile the test program using Javac:**
```
javac -cp . Test.java
```
Contributor guide
Assessment
This issue has not been assessed yet.