intellij-ideagroovygroovy-grapegroovyfx

How to grab a dependency and make it work with IntelliJ project?


I am trying to start a GroovyFX project within IntelliJ 12. However, I have not been able to get IntelliJ to compile and run the following simple script (for reproducing the problem in the simplest manner possible):

@Grab(group='org.codehaus.groovyfx', module='groovyfx', version='0.3.1')
import groovyx.javafx.GroovyFX
println GroovyFX.class.name

I used IntelliJ's support for Grape's Grab to add groovyfx as a dependency of my module (the jar is shown under "External Libraries" and the editor does not complain that the class is missing after that!) but still, when I run the script, it throws an error:

Groovyc: unable to resolve class groovyx.javafx.GroovyFX

I was able to get this script working in the GroovyConsole without much problem and as expected....

I tried grabbing another randomly chosen dependency (turned out to be a Spring library) and it worked straight out:

@Grab(group='org.springframework', module='spring', version='2.5.6')
import org.springframework.jdbc.core.JdbcTemplate
println JdbcTemplate.class.name

I can see no good reason why the Spring library should work, but not the GroovyFX library!!!!

I even double checked that the GroovyFx library had been actually downloaded, and it is there where it should be (under {user.home}/.groovy/grapes/{group}/{module}/jars/)

What can cause this weird and extremely frustrating problem??


Solution

  • I tested your problem both with groovyConsole (from groovy-sdk-2.1.0) and with IntelliJ IDEA 12.0.3. The only exception I got was:

    Caught: java.lang.NoClassDefFoundError: javafx/application/Application
    java.lang.NoClassDefFoundError: javafx/application/Application
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: java.lang.ClassNotFoundException: javafx.application.Application
        ... 1 more
    

    This was because the JavaFX runtime (jfxrt.jar) was not in the classpath. This can be fixed with

    mvn com.zenjava:javafx-maven-plugin:1.3:fix-classpath
    

    The command above is taken from the JavaFX Maven Plugin Wiki. You have to execute it only once. After that change both groovyConsole and IntelliJ are working. I had to restart the groovyConsole, this was not necessary for IntelliJ.

    What is interesting is that I don't see the GroovyFx-jar under "External Libraries". I am using plain IDEA community edition without any plugins.

    The Hello World from the GroovyFX homepage works out of the box in IDEA but not in the groovyConsole - I also get the "java.lang.ClassNotFoundException: groovyx.javafx.GroovyFX". I managed to let it run with the following code, but it is not a good solution, since it works only on the first run, then you have to restart the groovyConsole:

    @GrabConfig(systemClassLoader=true, initContextClassLoader=true)
    @Grab(group='org.codehaus.groovyfx', module='groovyfx', version='0.3.1')
    import static groovyx.javafx.GroovyFX.start
    
    start {
        stage(title: 'GroovyFX Hello World', visible: true) {
            scene(fill: BLACK, width: 500, height: 250) {
                hbox(padding: 60) {
                    text(text: 'Groovy', font: '80pt sanserif') {
                        fill linearGradient(endX: 0, stops: [PALEGREEN, SEAGREEN])
                    }
                    text(text: 'FX', font: '80pt sanserif') {
                        fill linearGradient(endX: 0, stops: [CYAN, DODGERBLUE])
                        effect dropShadow(color: DODGERBLUE, radius: 25, spread: 0.25)
                    }
                }
            }
        }
    }
    

    I am not sure but I think the reason is this bug here. Should be fixed in Groovy 2.2, we will see.