简体   繁体   中英

Strange and different behaviour across netbeans, eclipse and compiler

I have this code in a single file :

public class genIntro {
    public static void main(String [] args){
     genTest g = new genTest();
     g.add( 10 );
     System.out.println( g.get() == new Integer(10) ? true:false  );
     Integer in = (Integer) g.get();


    }
}

class genTest(){

        private Object object;

        public void add(Object object) {
            this.object = object;
        }

        public Object get() {
            return object;
        }


}

The second class genTest has a wrong declaration seen with the brackets () .

In Netbeans 6.9.1 the code runs correctly and outputs false .

Product Version: NetBeans IDE 6.9.1 (Build 201007282301)
Java: 1.6.0_21; Java HotSpot(TM) 64-Bit Server VM 17.0-b17
System: Windows 7 version 6.1 running on amd64; Cp1252; en_US (nb)
Userdir: C:\Users\Name\.netbeans\6.9

In Eclipse Indigo the code outputs :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at genIntro.main(genIntro.java:4)

Version: Indigo Service Release 1
Build id: 20110916-0149

Then manually compiling via the javac command I get :

    genIntro.java:12: '{' expected
    class genTest(){
         ^
    1 error

This is rather strange, can someone explain why the difference between them? Since it's erroneous why does it compile and run in Netbeans?

Running via javac genIntro.java

They all use jre6

Screenshot :

在此输入图像描述

I tried it with javac 7 from the command-line and NetBeans 7.1, and it gives the same error as your javac example in both. Are you sure the source is the same in your netbeans version? I can't see how it would compile at all.

Changing the line "class genTest(){" to "class genTest {" allows it to compile, and prints 'false'.

This is odd behaviour indeed. I created a new project in Netbeans (7.0.1) and added your code to it in a genIntro.java file. Some observations:

  1. The IDE does highlight the syntax error in your code
  2. Selecting Run the first time prompts the user that an error exists. You can choose to ignore this permanently, which I did.
  3. If you choose to ignore the syntax error the project appears to build and Run successfully
  4. In the bin folder for the project you can actually find a generated class file for the erroneous source. Reverse compiling this class shows that the syntax error has been removed .
  5. Back in the IDE, performing a Clean and Build (as opposed to a Run) generates the expected error:
Compiling 2 source files to /Users/tuoyo/Work/Data/Netbeans/Misc/build/classes
/Users/tuoyo/Work/Data/Netbeans/Misc/src/misc/genIntro.java:14: '{' expected
class genTest(){
1 error
/Users/tuoyo/Work/Data/Netbeans/Misc/nbproject/build-impl.xml:603: The following error occurred while executing this line:
/Users/tuoyo/Work/Data/Netbeans/Misc/nbproject/build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

Since Clean and Build invokes an Ant script behind the scenes I assume it is using the system JDK in console mode, which would jive with your original observations. Left unanswered is the question of how NetBeans is compiling the code when Run is selected - it does appear to be a different execution path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM