What is the expected behavior of the org.eclipse.jdt.core.compiler.problem.fatalOptionalError setting in org.eclipse.jdt.core.prefs supposed to be?
That setting or the associated "Treat above errors like fatal compile errors (make compiled code not executable)" option in the IDE under
Window > Preferences > Java > Compiler > Errors/Warnings
does not seem to have any effect whether it is turned on or off.
The weird thing is, the behavior is different depending on whether I use the eclipse IDE or the ecj.jar compiler from the commandline.
Looking at the documentation in the JDT source code (https://github.com/eclipse-jdt/eclipse.jdt.core/blob/0ba960caeb3c1ef51361af2db5ada06e11556744/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java#L1471):
/**
* Compiler option ID: Treating Optional Error as Fatal.
* <p>When enabled, optional errors (i.e. optional problems which severity is set to <code>"error"</code>) will be treated as standard
* compiler errors, yielding problem methods/types preventing from running offending code until the issue got resolved.</p>
* <p>When disabled, optional errors are only considered as warnings, still carrying an error indication to make them more
* severe. Note that by default, optional errors are not fatal. Non-optional errors are
* always fatal.</p>
* <dl>
* <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.fatalOptionalError"</code></dd>
* <dt>Possible values:</dt><dd><code>{ "enabled", "disabled" }</code></dd>
* <dt>Default:</dt><dd><code>"disabled"</code></dd>
* </dl>
* @since 3.2
* @category CompilerOptionID
*/
For me this means:
- enabled: Compile time error prevent the compilation from succeeding
- disabled: Compile time errors are still printed (but compilation continues) and at runtime an exception is thrown (the "Unresolved compilation problem" error).
However, in practice I was not able to reproduce this behavior.
I am using the following java class:
public class Test {
static int x = 12;
public static void main(String[] args) {
System.out.println(new Test().foo());
}
public int foo() {
return this.x;
}
}
And I set "Non-static access to static member" to "Error" (or org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error) to make the access in foo() produce an error.
- In the eclipse IDE: Running the program works, though there is a popup warning about the error. The class than crashes at runtime with the "Unresolved compilation problem" error. Even exporting the project as a JAR file works, there is simply a popup that says "JAR export finished with warnings. See details for additional information". The produced JAR crashes at runtime if fatalOptionalError is enabled, otherwise it runs without errors.
- In the commandline with the ecj.jar: Using the org.eclipse.jdt.core.prefs from the eclipse workspace, the compilation fails with an error about the static field access.
The behavior is exactly the same with fatalOptionalError enabled or disabled, except for whether the JAR file exported in eclipse actually runs.
Additional Context
Eclipse was installed on Ubuntu 23.04 through the snap store:
Version: 2022-12 (4.26.0)
Build id: 20221201-1913
For ECJ, I used ecj-4.28.jar and the following commandline:
java -jar ecj-4.28.jar -properties $HOME/eclipse-workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs Test.java
The mentioned org.eclipse.jdt.core.prefs looks like this (except for the fatalOptionalError, which I enabled and disabled for the tests):
eclipse.preferences.version=1
org.eclipse.jdt.core.codeComplete.visibilityCheck=enabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17