Home > Java, NetBeans, NetBeans Platform > NetBeans Platform – How to handle the working directory problem

NetBeans Platform – How to handle the working directory problem

I have written previously about NetBeans Platform, but as a refresher or an introduction to my new readers, it’s a Java based framework for developing rich desktop applications.  A NetBeans Project is comprised of one or more modules.  The code in Module A cannot be accessed by Module B, unless Module B declares a dependency on Module A, and Module A explicitly declares that its code is public.  In this way, code is segmented and encapsulated into logical blocks.

In this example, I’ve created an example project, with three modules – one for the View, Model, and Controller.  (See Wikipedia if that’s not ringing a bell, and then my post about a Java Solar system for a concrete example implementation).

The project can be run either from the main project node,

or by any of the modules:

While in most cases, the behavior will be identical, there is one way in which the manner in which you launch the application matters enormously: relative paths.
The working directory is directly influenced by whether you choose the project or the module to launch with:
# Application launched from the ExampleApplication node:
Current Directory       = /NetbeansExample/ExampleApplication

# Application launched from the View module:

Current Directory       = /NetbeansExample/ExampleApplication/View

Why is this a problem?

Say my View code has a properties file it needs to read in to initialize some code.  Let’s say I store it at
From within my sourcecode I might attempt to do the following:
File f = new File("resources/example.properties");
If I have started the application from the View module, then this will work fine; the current working directory is /NetbeansExample/ExampleApplication/View, as I previously said; thus by tacking on resources/example.properties, we come to a valid file.  If you launch from the main project, you will find that the file doesn’t exist, because /NetbeansExample/ExampleApplication/resources/example.properties does not exist.

How do can we handle this?

The best way I’ve found is through the use of the InstalledFileLocator class, provided by the Netbeans Platform Module APIs.

First, we need to move the .properties file from View/resources to View/release.

Once we have done that, we need to declare a dependency on the Module API in order to use this InstalledFileLocator class.  Right click on the View node and choose Properties,
In the libraries pane, click Add Dependency, and then start typing InstalledFileLocator.
The Module API should appear in the list.  Choose it and click OK.  You should see
From within your view code, you now replace code that looked like
File f = new File("resources/example.properties");


File f = InstalledFileLocator.getDefault().locate("example.properties", "net.developmentality.view", false);

where “example.properties” is the path to the file you’re trying to load, relative to the “release” folder, and where “net.developmentality.view” is the package name of the file in which you’re working.  The last argument is described in the Javadocs as:

localized – true to perform a localized and branded lookup (useful for documentation etc.)

I have had no problem leaving it false for my purposes.


I found this workaround after scouring the forums and being led to an FAQ about the LocalizedFileLocator.  It makes the code slightly more messy, but at least it works regardless of how the user or developer launches the application, which cannot be said for the standard way of specifying relative paths.  It is a very hard thing to search for, so hopefully someone running into this problem using NetBeans Platform finds this post.
  1. Chris Williams
    November 12, 2010 at 6:10 pm

    Thanks, this is exactly what I needed for a Netbeans Platform project I’m working on. Worked the first time.

    • i82much
      November 12, 2010 at 6:13 pm

      Great to hear! How did you find the post if you don’t mind my asking? And are there other NetBeans Platform things that you’d like me to write about?

    • Chris Williams
      November 14, 2010 at 10:15 pm

      Google search : “Netbeans platform reading properties file”

      One thing you may want to touch on is this only seems to be useful for reading from a properties file. As far as I can tell this can’t be used for writing back to the properties file if something needs to be updated. Writing to it just writes to \build\cluster

  2. March 27, 2013 at 4:26 pm

    We just started a new rcp project. We had an old pain swing app, very haard to maintain, and we start the porting some weeks ago.

    We realy was searching for a good method for file localization and this one is very cool!

    Found by google search.

    Lots of code to change right now, but very useful!!!!

    This info Will be shared to my developer teams!

  3. Javy Dreamer
    December 12, 2017 at 9:38 am

    Reblogged this on Coding Dreams.

  1. No trackbacks yet.

Leave a Reply to i82much Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: