mike192
mike192

Reputation: 91

Not loading css file in executable jar javafx8

I am getting an error while I try to run executable jar file.

lis 09, 2014 8:20:34 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged WARNING: Resource "/styles.css" not found.

When I'm running application from IDE, everything is OK. I try a several solution, but neither worked.

1.

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

2.

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

3.

scene.getStylesheets().addAll(new File("/styles.css").toURI().toString());

4

    InputStream inputStream = MainApp.class.getResourceAsStream("/styles.css");
    File tempStyleSheetDest = File.createTempFile("javafx_stylesheet", "");
    tempStyleSheetDest.deleteOnExit();
    Files.copy(inputStream, tempStyleSheetDest.toPath(), StandardCopyOption.REPLACE_EXISTING);
    scene.getStylesheets().add(tempStyleSheetDest.toURI().toString());

And another similar. I struggle this a few hours and I have no more ideas. I'm using maven, java1.8_u20.

Upvotes: 2

Views: 3356

Answers (3)

Marvin
Marvin

Reputation: 754

My sample executable jar with CSS:

Sample Output

through this file management, I did and resolve your problem. src |-drawable | |-bg.png | |-javaClass | |-mainApp.java | |-myStyle.css |

set external CSS file: scene.getStylesheets().add(getClass().getResource("myStyle.css").toExternalForm());

get external Image in other directory: -fx-background-image: url('../drawable/bg.png');

Upvotes: 0

JavierJ
JavierJ

Reputation: 559

If you are using maven, make sure to setup your resources folder correctly. Create a resource folder and copy your style.css file inside then add something like this to your pom.xml:

    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>resources</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

You have to make sure that the style.css file ends up inside the jar in the location you are requesting with the getClass().getResource() method (that's usually a relative path inside the class package folders or an absolute one if you add the "/" at the front). In Jens-Peter Haack defense, read this (from the getResource() javadoc) :

"If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e')." https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String-

So if you stick with the '/' at the front of the filename make sure your style.css file ends up in the root of your jar file, if not make sure it ends up in a path relative to the class location inside your jar.

Upvotes: 1

Jens-Peter Haack
Jens-Peter Haack

Reputation: 1907

Try

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

without the '/' in the beginning...

Upvotes: 1

Related Questions