3 Replies Latest reply on Feb 17, 2013 3:18 AM by baftos

    accessing .properties during development on Linux

      I have a program that normally runs as a .jar package. Within it are several proprety files, and it all works nicely regardless of OS.

      During development I sometimes run the main class directly. The properties files are located in the current directory. Everything still works fine in Windows.

      However when running Ubuntu Linux, the properties files are not being found. It is set to print out a message to console, and an example message is:
      loadProperties(): cannot access /cnd_ntc.properties
      It seems to be looking for the properties at the root level of the directory structure. What is the cross-platform way of accessing property files that will work both within a .jar file and without?

      The filenames are pre-pended with a "/" (e.g. "/CNDfilelist.properties"), which I had thought was the appropriate way to do it.

      For reference, here is the method I use to open all the .properties files:
          // encapsulate opening of a properties file.  returns null if problem
          public static Properties loadProperties(String file)
              String me = new String("loadProperties(): ");
              if (file == null) {
                  System.out.println(me + "null filename");
              Properties p = new Properties();
              InputStream fis;
              fis = NTC.class.getResourceAsStream(file);
              if (fis == null) {
                  System.out.println(me + "cannot access " + file);
              try {
              } catch (IOException e) {
                  System.out.println(me + "cannot load " + file);
                  try {
                  } catch (IOException e2) { /* what the heck you gonna do? */ }
              try {
              } catch (IOException e) { /* what the heck you gonna do? */ }
      -- Rich
        • 1. Re: accessing .properties during development on Linux
          Do you have a CLASSPATH environment variable in Linux? If yes, what is it?
          • 2. Re: accessing .properties during development on Linux
            That was it -- no CLASSPATH had been set.

            I just got Ubuuntu fully operational a couple weeks ago, and had its "Software Center" install the JRT and the IcedTea plugin. No JDK, and I have not done any development from the Linux side. The "java" command was in the executables' path and I did not think any further than that.

            Also, it was not obvious to me that the location of properties files would be related to the CLASSPATH variable.

            At any rate, thank you!

            Oh, I set it to
            This is similar to my Windows7 CLASSPATH. Would you suggest any modifications?
            • 3. Re: accessing .properties during development on Linux
              Good for you, but I am surprised a bit. I was expecting you to have a WRONG classpath in Linux. You see, in Windows I don't have a classpath at all. From all I know, if no classpath is specified by the environment variable or by the java command line, it defaults to the current directory. Looks like this is true in Windows, but not in Linux. Anyway, if you are unclear about how getResourceAsStream() locates resources, it uses the classpath, explicit or not, and not the file system for loading resources.

              Edited by: baftos on Feb 16, 2013 10:18 PM