5 Replies Latest reply on Nov 22, 2012 2:19 PM by jensh

    How to start a JavaFX 2 application using Weld SE?

    marc.walter

      I would like to use Weld in order to get all those nice CDI features.

      As you know JavaFX 2 applications are started using the static Java main method:

       

      public static void main(final String[] args) {
          launch(args); // launches JavaFX
      }
      

       

      Now Weld SE uses a different main method for bootstrapping:

       

      public void main(@Observes ContainerInitialized event) {
          launch(); // is this right?
      }
      

       

      I hoped placing the launch() method into the Weld bootstrapper is all to get things moving.

      Next I executed the commend line mvn exec:java -Dexec.mainClass="org.jboss.weld.environment.se.StartMain" in my project root in order to start my application but all I got was this:

       

      45 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.Version - WELD-000900 1.1.6 (Final)

      160 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.

      424 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.LeagueController$1 because of underlying class loading error

      426 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.LeagueController$2 because of underlying class loading error

      427 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.LeagueController$3 because of underlying class loading error

      429 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.LeagueController because of underlying class loading error

      430 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.MainController because of underlying class loading error

      432 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$1 because of underlying class loading error

      433 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$2 because of underlying class loading error

      436 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$3 because of underlying class loading error

      437 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$4 because of underlying class loading error

      438 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$5 because of underlying class loading error

      439 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$6 because of underlying class loading error

      441 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController$7 because of underlying class loading error

      443 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.controller.TeamController because of underlying class loading error

      444 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.CountryValueFlagCell because of underlying class loading error

      445 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.Application because of underlying class loading error

      447 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.PlayerContentProvider because of underlying class loading error

      457 [org.jboss.weld.environment.se.StartMain.main()] INFO org.jboss.weld.ClassLoading - WELD-000119 Not generating any bean definitions from gui.SkillValueColorCell because of underlying class loading error

      543 [org.jboss.weld.environment.se.StartMain.main()] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled

      544 [org.jboss.weld.environment.se.StartMain.main()] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

      [INFO] ------------------------------------------------------------------------

      [INFO] BUILD SUCCESS

      [INFO] ------------------------------------------------------------------------

      [INFO] Total time: 2.132s

      [INFO] Finished at: Sat Apr 21 00:00:24 CEST 2012

      [INFO] Final Memory: 12M/216M

      [INFO] ------------------------------------------------------------------------

       

      So what am I doing wrong? Did I miss something? Why do I get underlying class loading errors?

      Thank you for your help!

        • 1. Re: How to start a JavaFX 2 application using Weld SE?
          marc.walter

          I managed to start my application with the following line:

           

          public static void main(final String[] args) {
              new StartMain(args).go();
              launch(args);
          }
          

           

          But this does not seem to be the right way because injection points are not initialised properly. I get a NullPointerException when I want to call my service. :-(

           

          At startup I get two warnings. Can I ignore them?

           

          973 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled

          974 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

          • 2. Re: How to start a JavaFX 2 application using Weld SE?
            marc.walter

            Apparently I'm talking to myself here... well maybe someone can find my monologue useful...

             

            In the meantime I found out that as soon as the Weld container starts it parses the classes for methods with the @Observes ContainerInitialized event annotation. If it finds one an instance of the class is created and the injection points are initialized. So at JavaFX application startup all injection points are initialized (if the method outlined above is present in the class).

             

            Now in my JavaFX application all screens and views are built using FXML. When a view is loaded into memory with FXMLLoader.load() a new controller instance is created. In this case Weld does not do anything (maybe it is sleeping...) and so the injection points are not initialized. So in my opinion Weld is not your friend if you're using FXML.

             

            In order to access the controller defined in and initialized by FXML you can do this:

             

            final FXMLLoader loader = new FXMLLoader();
            loader.load(getClass().getResourceAsStream("/fxml/main.fxml"));
            final MainController mainController = (MainController) loader.getController();
            

             

            But what about my service I need in my MainController? Well I'm afraid you have to manually create your service and set it into the controller. Weld won't help you here.

             

            final MyService service = new MyServiceImpl();
            mainController.setService(service);
            

             

            I'm sorry I have no better solution for you. I would have loved to use Weld here but it simply does not work (or I did not find the right way). Maybe there will be support for JavaFX in one of the next major Weld releases or someone of the Weld developer team reads this thread some day and is willing to post an answer...

            • 3. Re: How to start a JavaFX 2 application using Weld SE?
              alesj

              No need for smart arse comments at the end of this *free* forum. ;)

              I'll have a look at this after the May holidays.

              • 4. Re: How to start a JavaFX 2 application using Weld SE?
                marc.walter

                Well this forum may be free but these are the *official* JBoss community forums so I expect someone of the JBoss team to have a look at it. I do not expect answers within 5 minutes. But I'm accustomed to some kind of response from other JBoss forums. If someone would say "Weld does not support JavaFX" this would be okay for me. I spent hours getting Weld to work with JavaFX and I can say it is very frustrating getting NPEs all the time.

                 

                I appreciate that you'll have a look at my issue. Thank you.

                • 5. Re: How to start a JavaFX 2 application using Weld SE?
                  jensh