Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-6062

Static block not executed in Java Ant project

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 12.4
    • None
    • None
    • None
    • NetBeans 12.4

      Java 15.0.2 (Adopt Open JDK)

      Windows 10

    Description

      A simple Java Ant project in NetBeans 12.4 (using Java 15) does not execute the static code block, when the project is executed from within the IDE.

      Steps to reproduce:

      File > New Project > Java with Ant

      Create two classes:

      (Whenever I try to format the below code, it looks completely wrong in the ticket, so it's just presented as plain text - sorry I do not know how this system handles code snippets).

      1) Main.java

      public class Main {
           public static void main(String args[]) {
               System.out.println(Test.i);
           }
      }
      

      2) Test.java

      class Test {
           static int i;
           static {
               i = 10;
           }
      }
      

       

      Actual results:

      When the project is run from within the IDE, the output is "0". The static block in Test is not executed.

      Expected results:

      The expected output is "10". The static block in Test is executed.

      Note also:

      The JAR built by the project runs correctly, when executed from the command line, outside of the IDE.

      The same code shown above also runs as expected in the IDE when built using a Java Maven project, instead of a Java Ant project.

      This behavior does not appear when executing the same code, using NetBeans 12.0 and Java 11.

      Attachments

        1. NETBEANS-6062.zip
          18 kB
          Benjamin Asbach

        Issue Links

          Activity

            People

              Unassigned Unassigned
              andrewjames Andrew James
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: