4

I have a java swing database application which needs to be run on Windows and Linux. My database connection details are stored in a XML file and I load them.

This application can load this properties on Linux properly but it is not working on Windows.

How do I load files on multiple platforms properly using Java?


This is the code:

PropertyHandler propertyWriter = new PropertyHandler();

List keys = new ArrayList();
keys.add("ip");
keys.add("database");
Map localProps = propertyWriter.read(keys, "conf" + File.separatorChar + "properties.xml", true);//if false load from the local properties

//get properties from the xml in the internal package
List seKeys = new ArrayList();
seKeys.add("driver");
seKeys.add("username");
seKeys.add("password");

Map seProps = propertyWriter.read(seKeys, "conf" + File.separatorChar + "properties.xml", true);

String dsn = "jdbc:mysql://" + (String) localProps.get("ip") + ":3306/" + (String) localProps.get("database");
jDBCConnectionPool = new JDBCConnectionPool((String) seProps.get("driver"), dsn, (String) seProps.get("username"), (String) seProps.get("password"));

File reader method:

public Map read(List properties, String path, boolean isConfFromClassPath)
{
    Properties prop = new Properties();
    Map props = new HashMap();
    try {

        if (isConfFromClassPath) {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();

        } else {
            FileInputStream in = new FileInputStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return props;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Harsha
  • 3,548
  • 20
  • 52
  • 75

5 Answers5

5

If the file is in a jar file and accessed by the classpath then you should always use /.

The JavaDocs for the ClassLoader.getResource say that "The name of a resource is a '/'-separated path name that identifies the resource."

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

yiannis
  • 1,421
  • 12
  • 21
1

I'm not sure if there is the proper way, but one way is:

File confDir = new File("conf");
File propFile = new File(confDir, "properties.xml");

But in a scenario as simple as yours, I would just use /

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • Not what I would have recommended, but still a nice and creative workaround. :) – Bananeweizen May 15 '12 at 05:15
  • 1
    I would not recommend this approach for this scenario as well, but there are others, for instance reading multiple files in a directory that was passed as an argument. – Cephalopod May 15 '12 at 10:58
0

If it's a resource located in classpath, we can load it with following snippet:

getClass().getClassLoader().getResourceAsStream(
    "/META-INF/SqlQueryFile.sql")));
James Gan
  • 6,988
  • 4
  • 28
  • 36
0

You can load all files on multiple platforms without any trouble.

Kindly use Matcher.quoteReplacement(File.separator) for replacing the slash.

It will works for every platform.

String fileLocation = "/src/service/files";

fileLocation  = fileLocation.replaceAll("/",Matcher.quoteReplacement(File.separator));
Radadiya Nikunj
  • 988
  • 11
  • 10
-1

assuming that your file is in conf/properties.xml on Linux and conf\properties.xml on Windows, use File.pathSeparator instead of File.separator

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67