1

Eclipse happily generates bytecode for Java classes that have compilation errors. The offending methods just throw exceptions when invoked at runtime.

Does anyone know how I can stop Eclipse from generating bytecode when compilation errors are present? I'd rather not have runnable code at all in the presence of errors.

As an example, consider the following code:

public class Err {
  public static void main(String[] args) {
    methodWithoutCompilationError();
    // methodWithCompilationError();
  }

  private static void methodWithoutCompilationError() {
    System.out.println("No error here, folks.");
  }

  private static void methodWithCompilationError() {
    System.out.println("This method doesn't compile." + );
  }
}

It runs fine, even with the compilation error. Only when I uncomment the second line of main do I see there was a problem compiling.

kaerimasu
  • 690
  • 3
  • 17
  • 3
    I have not seen such behavior. Can you clarify your build process through eclipse? – Sotirios Delimanolis Feb 28 '14 at 22:13
  • 2
    Did you select some sort of "run anyway" option? – user2357112 Feb 28 '14 at 22:14
  • More likely what you've got is leftover classfiles from the last successful compilation... – keshlam Feb 28 '14 at 22:35
  • Are you sure you're not talking about warnings, rather than errors? – Hot Licks Feb 28 '14 at 22:37
  • 1
    This is a by product of the incremental compiler. You would get about 100 warnings if you actually tried to run, build, or deploy a project with compile errors in it. – BrianC Feb 28 '14 at 22:49
  • As @BrianC suggests, this is how Eclipse works. See http://stackoverflow.com/q/16394152/540048. However, the runtime exceptions that are inserted only are thrown if the offending code is invoked. I want to know if the behavior of generating exception-laden bytecode is just a default that I can adjust. – kaerimasu Mar 01 '14 at 07:26

3 Answers3

1

The reason Eclipse does this is that the compile errors might be resolved by editing another java source file. For example, if a reference to a method name in another class is misspelled, the fix could be that you correct the spelling of the reference or the fix could be that you change the name of the method. Eclipse doesn't know which you'll choose to do so it compiles the referencing class anyway in case you decide to change to other file. I'm not even sure it could otherwise know when to compile all of the classes again.

As a result, Eclipse will always compile the edited java source to the extent possible whenever you change the source file.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
0

I'm guessing at some point you enabled the 'Continue launch if project contains errors' option. Try setting it to 'Prompt':

enter image description here

martinez314
  • 12,162
  • 5
  • 36
  • 63
  • This answers a different question. This setting determines whether or not I can run bytecode with unresolved compilation errors through Eclipse. Whatever this is set to, the bytecode is still produced and looks executable outside of Eclipse. – kaerimasu Mar 01 '14 at 07:28
0

Eclipse compiler preferences - Errors/Warnings

Try checking the "Treat above errors like fatal compile errors (make compiled code not executable)" checkbox.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • I had seen this but I find it had no effect. Even with it checked, I run the example I've added to my question without any trouble even though it doesn't fully compile. – kaerimasu Mar 01 '14 at 07:38