0

Here is my project structure

  DejaVuu
    -- src/main/java
    ---- views 
    ------ basetheme.css
    -- src/test/java
    -- src
    -- basetheme.css
    -- tempfile.css

Currently I am trying to read in my css file with

view.getStylesheets().add("basetheme.css"); 

But this does not work. However, it works when I use

view.getStylesheets().add("views/basetheme.css");

I need basetheme.css at the root because that is where I rewrite the file to after some updates. In other words, the read and write location should be the same.

Here is the code I call to write to the file:

    public String writeToCSS(String css) throws IOException {
        String path_name = "basetheme.css"; // I need this to match read in for getStyleSheet().add
        File temp = new File(path_name); 
        temp.delete();
        temp.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
        bw.write("some css");
        bw.close();
        
   
        String temp_url = temp.toURI().toString();
        return temp_url; 
    }

Now when looking in writeToCSS and the following is set

String path_name = "basetheme.css";

the above writing works. This is to root of project. The issue is that now I cannot read the stylesheet into JavaFX from this location.

With that said, I have tried to write the file to

String path_name = "/home/ming/eclipse-workspace/DejaVuu/views/basetheme.css";

However, I receive an error when doing this

java.io.IOException: No such file or directory
    java.io.UnixFileSystem.createFileExclusively(Native Method)
    java.io.File.createNewFile(File.java:1012)

Can File not be used to write to packages in MavenProjects?

Xenon
  • 311
  • 5
  • 12
  • It appears that the path I copy and pasted from the file itself is the incorrect path. I did not second guess the path generated, but I just realized this and fixed it, and now I can write to there. – Xenon Nov 24 '20 at 09:26
  • 1
    The path you specify is relative to the *classpath*. What's important is what is available at runtime (when the CSS is actually loaded); i.e. what's important is what's in the build, not the source structure. Check the contents of `target/classes` (the default build folder for Maven) or the jar file if you are building a jar file to understand that layout. It's likely that resources in the project that are outside `src/main/java` or `src/main/resources` simply aren't deployed at all (unless you specifically configure Maven to deploy them). – James_D Nov 24 '20 at 13:29
  • 1
    See also https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Nov 24 '20 at 13:30

0 Answers0