0

This is the structure for my Gradle project:

- Game [parent]
 |- Server [module]
    -- Cache
 |- Client [module]

The problem occurs when I try to access the cache from my server java project using:

new File("/cache/cacheItem.dat");

Java will now look in the parent folder (Game) for the cache instead of the server folder and I don't know how to change the root for the server project using Gradle. I would like to know how to change the root directory of the server to it's own folder instead of the parent.

1
  • For me it seems obvious that the absolute path doesn't use the parent as base and I am wondering if I can change the relatiev path to be server instead. Commented Jan 3, 2020 at 7:41

1 Answer 1

1

You should not rely on your project structure for accessing and especially writing files. If you package your application for deployment into JARs, any of these paths will no longer be available to write data to and may change even during development, as you have noticed. Instead, you should make your cache path configurable, e.g. from a configuration file and/or command line parameter. This allows the cache path to be configured to an external location upon deployment and to some temporary path during development.

If you only need read access to files at a known location place them under src/main/resources (or src/test/resources for tests). Gradle by convention packages the content of this directory together with your code and everything therein will be available at runtime at root level, during development as well as after deployment. To access such resources on the classpath in Java use getClass().getResource("path/to/resource").

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.