aaronc / fx-clj

A Clojure library for JavaFX
http://documentup.com/aaronc/fx-clj
Eclipse Public License 1.0
107 stars 9 forks source link

uberjar and compile #5

Open ghost opened 9 years ago

ghost commented 9 years ago

Anyone else has problems with uberjar and compile? It just won't complete. I have to use a Runner class as a workaround and run my clojure code without AOT which results in long startup times when I run the jar file :(.

public class Runner {

  public static void main(String[] args) {
    clojure.lang.IFn require = clojure.java.api.Clojure.var("clojure.core", "require");
    require.invoke(clojure.lang.Symbol.intern("my-gui.core"));
    clojure.lang.IFn show = clojure.lang.RT.var("my-gui.core", "show");
    show.invoke();
  }
}
; ------------------------------------------------------
(ns my-gui.core
  (:require [fx-clj.core :as fx]))

(defn view []
  (fx/compile-fx ...........))

(defn show []
  (fx/sandbox #'view))
; ------------------------------------------------------
(defproject
  ... NO AOT
  :main Runner
  ...)
KPCCoiL commented 8 years ago

I also encountered this issue. According to this discussion (https://groups.google.com/forum/m/#!topic/leiningen/s9CBKp832YE), initializations are executed while AOT. I'm not sure about that, I think it's a problem of fx-clj. Maybe some patches will be needed.

aaronc commented 8 years ago

Can anyone post a stack trace? On Wed, Sep 16, 2015 at 3:56 AM KPCCoiL notifications@github.com wrote:

I also encountered this issue. According to this discussion ( https://groups.google.com/forum/m/#!topic/leiningen/s9CBKp832YE), initializations are executed while AOT. I'm not sure about that, I think it's a problem of fx-clj. Maybe some patches will be needed.

— Reply to this email directly or view it on GitHub https://github.com/aaronc/fx-clj/issues/5#issuecomment-140660040.

KPCCoiL commented 8 years ago

I don't know how to display the stack trace, but I could cause the problem with this tiny project:

project.clj:

(defproject fx-clj-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0-alpha2"]
                 [fx-clj "0.2.0-alpha1"]]
  :main ^:skip-aot fx-clj-test.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

src/fx_clj_test/core.clj:

(ns fx-clj-test.core
  (:require [fx-clj.core :as fx])
  (:gen-class))

(defn make-view []
  (fx/button "Hello!"))

(defn -main [& args]
  (fx/sandbox #'make-view))

When I executed lein uberjar in this project, "Compiling fx-clj-test.core" appeared and it freezed. In other hand, lein run works fine.

aaronc commented 8 years ago

I was able to create an uberjar but that was a while ago and it took some tweaking. My issue was related to a dependency that was causing problems, but I've since replaced that dependency. Unfortunately, I haven't been working with this code base for a while and don't really have a lot of time right now. Maybe someone else can provide some insight?

On Wed, Sep 16, 2015 at 10:23 AM, KPCCoiL notifications@github.com wrote:

I don't know how to display the stack trace, but I could cause the problem with this tiny project:

project.clj:

(defproject fx-clj-test "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.8.0-alpha2"] [fx-clj "0.2.0-alpha1"]] :main ^:skip-aot fx-clj-test.core :target-path "target/%s" :profiles {:uberjar {:aot :all}})

src/fx_clj_test/core.clj:

(ns fx-clj-test.core (:require [fx-clj.core :as fx]) (:gen-class))

(defn make-view )

(defn -main [& args](fx/sandbox #'make-view))

When I executed lein uberjar in this project, "Compiling fx-clj-test.core" appeared and it freezed. In other hand, lein run works fine.

— Reply to this email directly or view it on GitHub https://github.com/aaronc/fx-clj/issues/5#issuecomment-140756423.

QAston commented 8 years ago

For me the line https://github.com/aaronc/fx-clj/blob/master/src/fx_clj/impl/bootstrap.clj#L4 prevents AOT, I've commented it out, called it in a fn during app startup and everything works.

birdspider commented 8 years ago

might (ns ^:no-doc ^:skip-aot fx-clj.impl.bootstrap) work ?

retrogradeorbit commented 7 years ago

I have hit this problem. The ^:skip-aot did not solve the compilation hang. Removing the line entirely made the compile succeed. But then adding it into the main application made the compile hang again in the main application. Eventually I got the compile working by moving (javafx.embed.swing.JFXPanel.) into the actual mainline code. But then there is a different problem. lein run works. And lein uberjar compiles. But running the jar file with java command line doesn't work giving: java.lang.IllegalStateException: Toolkit not initialized

QAston commented 7 years ago

You need to call (javafx.embed.swing.JFXPanel.) before you use javafx. You can see a working example here: https://github.com/QAston/URDMI/blob/master/src/urdmi/gui_util.clj (see init-toolkit fn)

QAston commented 7 years ago

I just remembered that some controls may have static initializers that cause this error. In that case you need to call the static initializer later (lookup the class dynamically instead of refering to it statically from clojure).

retrogradeorbit commented 7 years ago

I did call (javafx.embed.swing.JFXPanel.). Does your working example uberjar? To reiterate again:

  1. When (javafx.embed.swing.JFXPanel.) is defonced, lein uberjar hangs
  2. When (javafx.embed.swing.JFXPanel.) is put inside the mainline, lein uberjar 'works', but the compiled jar when run directly from java -jar doesn't work, raising a java.lang.IllegalStateException: Toolkit not initialized. (lein run DOES work with this method, just the packaged jar fails)

I have given up on this approach and am now building my JavaFX app by directly extending the JavaFX Application classes via gen-class, as you would build it in Java. When I do this it all works, including the uberjar running under java -jar.

QAston commented 7 years ago

This is an example of a launcher class that works in AOT and is written in clojure https://github.com/QAston/URDMI/blob/master/src/urdmi/launcher.clj