【问题标题】:Scene.getStylesheets().add() not working inside jar fileScene.getStylesheets().add() 在 jar 文件中不起作用
【发布时间】:2013-04-23 14:46:18
【问题描述】:

只要我直接从 Eclipse 运行我的项目,我就没有问题:

scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm());

但只要我在 jar 文件中运行此代码,就找不到资源 (NullPointerException)。

我尝试将css 文件移动到我的src 文件夹,然后只将stylesheet.css作为路径而不是/stylesheet.css,但这会导致同样的问题:使用Eclipse 可以正常工作,但不能从jar 中运行。

提示:我正在使用Zonskis Maven JavaFX Plugin 来生成jar。

【问题讨论】:

    标签: java jar resources javafx-2 maven-3


    【解决方案1】:

    我只是浪费了我(你的)时间来编写愚蠢的 ma​​ven 个人资料。

    而不是:

    scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm());
    

    简单写:

    scene.getStylesheets().add("stylesheet.css");
    

    这就是Zonski 加载css 文件的方式。

    当然,您的stylesheet.css 文件应该在/src/main/resources 中,或者在CLASSPATH 上的某个位置。

    【讨论】:

    • 大声笑,有趣,稍后会验证这一点,然后选择此答案作为正确答案;-) 感谢您的第二次回复!
    • 在 /src/main/resources 中使用 css 的每个人请注意:您必须确保 css 文件位于资源文件的根目录中,而不是项目包的根目录中(例如 /src/main/resources /com/myorg/myapp)。
    【解决方案2】:

    将您的文件移动到src/main/resources 并添加您的css 文件:

    scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheet.css").toExternalForm());
    

    好吧,如果你想从 jar 中运行它,那么将 stylesheet.css 更改为 stylesheet.bss (binary css),打包你的应用程序:

    mvn clean compile jfx:build-jar
    

    然后运行你的 jar。

    java -jar app.jar
    

    我有一个丑陋的 hack 使它有点可用(我正在使用 Netbeans,令人惊叹的 ma​​ven 完整性):

    我在src/main/resources 目录中创建了一个project.properties 文件,

    file_css= ${file_css} // by default I use *.css file.
    

    并使其可过滤,在我的POM 文件中:

    ...
        <build>     
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>        
                <plugins>           
                    <plugin>
                        <groupId>com.zenjava</groupId>
                        <artifactId>javafx-maven-plugin</artifactId>
                        <version> 1.5 </version>
                        <configuration>          
                                        ....
                        </configuration>
                    </plugin>
                </plugins>
    </build>
    
    ...
    

    并创建两个maven 配置文件,一个用于dev,另一个用于production(打包到jar):

    <profiles>
            <profile>
                <id>production</id>
                <properties>
                    <file_css>stylesheet.bss</file_css>
                </properties>
            </profile>      
            <profile>
                <id>dev</id>
                <properties>
                    <file_css>stylesheet.css</file_css>
                </properties>
            </profile>
    </profiles>
    

    所以,你可以像这样加载你的css 文件:

    scene.getStylesheets().add(getClass().getClassLoader().getResource(ResourceBundle.getBundle("project").getString("file_css")).toExternalForm());
    

    我使用production 配置文件进行打包,dev 用于通常的操作,例如compile, test, run


    编辑: 一个完整的例子托管在github

    【讨论】:

    • 感谢您的回答,但仍然是同样的问题):从 Eclipse 运行时可以正常工作,但不能从 JAR 运行。
    • 谢谢,从 JAR 运行时有效!但是有没有一种智能的方法可以在构建 JAR 文件时自动使用 bss 并在其他情况下使用 css?因为使用 bss 从 Eclipse 运行时它不会工作 \:
    • @stefan.at.wpf 如果我的解决方案看起来很难看,请尝试打开issue
    • 不,感觉这是一种易于实施的方式,只需要一些时间来测试它;-) 但是现在接受你的答案,因为你回答了原来的问题 :-) 非常感谢又来了!
    • tarrsalah,如果缺少过滤的东西,你能看看你的答案和你的 POM 吗?您到底在使用哪个命令行命令?我从来没有把我的变量替换为 stylesheet.bss ):
    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多