0

I've a Java application which depends upon local source binaries, such as fonts and images. Whilst developing in Eclispe, I made use of a /res directory within my /src to house these dependencies, resulting in an example project structure would look like this:

- src
    - res
        lena.png
    - com.foo
        Main.java

Within Main.java, the following invocation could be used to acquire a resource reference.

final File lFile = new File(Main.class.getClassLoader().getResource("res/lena.png")).getPath());

This approach has served me well throughout my development, but since moving to IntelliJ it results in a FileNotFoundException. This is the only aspect of the application that has ceased to function since the transition.

Do dependencies need to be registered in a different manner than simply inserting them into the project structure when using IntelliJ? Is there an IDE-independent approach that I should be using instead?

Here's the Exception; it looks like it's pointing towards the compiled directory (/bin) but I could be mistaken:

java.io.FileNotFoundException: /home/alexander/Development/application-1/application/bin/res/fonts/lena.png (No such file or directory)

I modified the Project from using Inherit Project Compile Output Path to use Compile to Module Output Path, which changed the output directory from Eclipse's /bin to IntelliJ's /out. The runtime path updated accordingly. I still fail to access the files, though I can confirm they exist within the /out hierarchy, and they point to the correct location.

Mapsy
  • 4,192
  • 1
  • 37
  • 43
  • Did you add `bin` folder to classpath? – Keerthivasan Mar 21 '16 at 13:41
  • Guess you want to check on resource bundles and such stuff. Normally any kind of resource should be part of some package; and the IDE would then copy it to your "overall" root/bin or root/output directory. In other words: having a dedicated bin folder under src is imho **not at all** what you should be doing. – GhostCat Mar 21 '16 at 13:42
  • Eclipse and IntelliJ (by default) use different folders for output. So, you probably want to ensure that you're not referencing a resource from within an output folder. The root of your path should be within the output folder itself to ensure IDE agnosticism. – ManoDestra Mar 21 '16 at 13:47
  • @Keerthivasan: I just tried adding the folder using the `Project Settings`, but this seems to be suitable for source files only. Am I doing this wrong? – Mapsy Mar 21 '16 at 13:47
  • @Jägermeister: I misunderstand slightly; are you suggesting that I should place the `bin` folder within `com.foo`? Or should I create a separate module altogether? – Mapsy Mar 21 '16 at 13:50
  • @ManoDestra This sounds like the likely culprit. I'll add the resulting path to my question. – Mapsy Mar 21 '16 at 13:53
  • Check out the other SO question - http://stackoverflow.com/questions/854264/how-to-add-directory-to-classpath-in-an-application-run-profile-in-intellij-idea – Keerthivasan Mar 21 '16 at 13:58
  • Updated the question. I'll check this out. – Mapsy Mar 21 '16 at 13:59
  • @Keerthivasan No dice, unfortunately! Same error. I must admit it looks strange referencing the dependencies of a nested subpackage of the Project on the build path... – Mapsy Mar 21 '16 at 14:08
  • @AlexT. Yes, typically your resources are a subfolder somewhere within your source code packages. – GhostCat Mar 21 '16 at 14:48
  • Have you recognized the mismatch between your src directory structure and the error message? Your program is looking for the file in the **bin/res/fonts/** directory which is not reflected in your src directory structure. And look at in the **bin** directory if the file exists there and under which directory. – ujulu Mar 21 '16 at 14:59
  • @ujulu My understanding is that the project is compiled to the `/bin` directory, so `/src` becomes `/bin`; then the structure doesn't change. What's weird about this is that I've specified the absolute path of my dependencies and it still throws an error! I must be missing something really fundamental. – Mapsy Mar 21 '16 at 15:15
  • I just thought of something; there's spaces in my file path. I'll put my money on that being the issue... – Mapsy Mar 21 '16 at 15:17

0 Answers0