0

Since I changed the structure of my Java source packages, some of the file open functions don't want to work anymore. I moved some resources (icon, properties files, etc) from MyProject\res\ to MyProject\src\res\.

I think I should use this.getClass().getResourceAsStream("icon.png") instead of file path string but I'm not sure.

Here is how I access to the icon file before I change the resources location:

private final static String iconSourcePath = "file:res/icon/icon.png";

primaryStage.getIcons().add(new Image(iconSourcePath));

Is there a rule for access to files in Java? If the requested file is in the source file or outside?

Edit: This is the solution I found.

I found the easiest way is to put allways absolute paths, relative to the src folder.

For example, in my project I have this folder hierarchy:

src

  META-INF
    MANIFEST.MF

  res
    icon
      icon.png
    lang
      MessageBundle
      MessageBundle_en.properties
      MessageBundle_fr.properties
    pref
      AppProperties
      Properties.properties

  simulation
    log
      Console.java
    views
      settingsWindows
        settingsWindows.fxml
        SettingsWindows_Controller.java
      simulation
        simulation.fxml
        Simulation_Controller.java
      toolBar
        toolBar.fxml
        ToolBar_Controller.java
    Simulation.java

  Main.java

If I need add the icon.png in the SettingsWindows_Controller.java (in JavaFX), I will do this like that:

private final static String iconSourcePath = "/res/icon/icon.png";

stage.getIcons().add(new Image(iconSourcePath));

And if I want to load the toolBar.fxml in any file, I will do that:

private final static String fxmlSourcePathToolBar = "/simulation/views/toolBar/toolBar.fxml";

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxmlSourcePathToolBar));

With this methode, it will works in debug mode (in the IDE) AND in the JAR file.

Jeankowkow
  • 814
  • 13
  • 33

1 Answers1

0

Better you should use getResourceAsStream function to get current class location. For Example :

ImageFactory.class.getResourceAsStream("image.png");

Above code loads image present in the folder, where ImageFactory class is present, if your images are in different folder, then use .. and / operators to go previous folder and get child folder respectively.

Sandeep Kokate
  • 825
  • 4
  • 16
  • Is it possible to use an absolute path from the root package? Like `src/res/...`, without the `../`? – Jeankowkow Jul 19 '16 at 11:20
  • @ArmandDelessert : `System.getProperty("user.dir")` helps you to get absoute path for the current project. just one folder behind your root package. – Sandeep Kokate Jul 19 '16 at 11:34
  • It doesn't work. I try with `getClass().getResource(fxmlSourceName)` to get a valid URL but it returns always `null` when I run the JAR file. In debug mode it works fine... I don't understand the logic behind the file path/name mechanism. – Jeankowkow Jul 20 '16 at 08:36
  • @ArmandDelessert : Have you checked that your jar contains all images in right folder? It may possible that your build script only bind java files into the jar not images. Try to open jar with winzip and check images are present in that. – Sandeep Kokate Jul 20 '16 at 08:38
  • I generate the JAR file with IntelliJ default settings, it contains all files in the `src`. But I found an easy and reliable way to do that. – Jeankowkow Jul 20 '16 at 09:31
  • @ArmandDelessert : Have you tried with opening your jar and finding the images in it? – Sandeep Kokate Jul 20 '16 at 10:17
  • Yes, the `icon.png` is located in the `src/res/icon/` folder. All of this is in the JAR (I checked by opening the JAR file). – Jeankowkow Jul 20 '16 at 11:10