This discussion is archived
3 Replies Latest reply: Feb 16, 2013 7:18 PM by baftos RSS

accessing .properties during development on Linux

801338 Newbie
Currently Being Moderated
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");
            return(null);
        }

        Properties p = new Properties();
        InputStream fis;

        fis = NTC.class.getResourceAsStream(file);
        if (fis == null) {
            System.out.println(me + "cannot access " + file);
            return(null);
        }

        try {
            p.load(fis);
        } catch (IOException e) {
            System.out.println(me + "cannot load " + file);
            try {
                fis.close();
            } catch (IOException e2) { /* what the heck you gonna do? */ }
            return(null);
        }
        try {
            fis.close();
        } catch (IOException e) { /* what the heck you gonna do? */ }

        return(p);
    }
-- Rich
  • 1. Re: accessing .properties during development on Linux
    baftos Expert
    Currently Being Moderated
    Do you have a CLASSPATH environment variable in Linux? If yes, what is it?
  • 2. Re: accessing .properties during development on Linux
    801338 Newbie
    Currently Being Moderated
    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
    .:classes:/media/DATA/progming/java/projects/classes
    This is similar to my Windows7 CLASSPATH. Would you suggest any modifications?
  • 3. Re: accessing .properties during development on Linux
    baftos Expert
    Currently Being Moderated
    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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points