Strange and different behavior through netbeans, eclipse and compiler

I have this code in one 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 genTest class has an invalid declaration in parentheses () .

In NetBeans 6.9.1, the code works correctly and returns 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 output is:

 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 with the javac command, I get:

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

This is rather strange, can someone explain why the difference is between them? Since it is wrong, why does it compile and run on Netbeans?

Running through javac genIntro.java

They all use jre6

Screenshot:

enter image description here

+4
source share
2 answers

I tried this 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 that the source in your version of netbeans has not changed? I donโ€™t see how this compiles at all.

Changing the line "class genTest () {" to "class genTest {" allows you to compile and print "false".

+2
source

This is a weird behavior. I created a new project in Netbeans (7.0.1) and added the code in the genIntro.java file to genIntro.java . Some observations:

  • The IDE emphasizes the syntax error of your code.
  • Selecting "Run" for the first time causes the user an error. You can ignore it all the time, which I did.
  • If you decide to ignore the syntax error that the project creates and successfully execute
  • In the bin folder for the project, you really can find the generated class file for the erroneous source. Reverse compilation of this class indicates that the syntax error has been removed .
  • Back in the IDE, running Clean and Build (as opposed to 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 the Ant script behind the scenes, I assume that it uses the system JDK in console mode, which will live with your initial observations. An unanswered question about how NetBeans compiles code when Run is selected - it looks like a different execution path.

+1
source

Source: https://habr.com/ru/post/1394889/


All Articles