views:

1172

answers:

3

Fixed the problem with the following code - not the most gracious way - but i need a quick solution for this assignment

package six.desktop.gui.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Config
{
    private static final String configFileName = "/config/config.ini";

    private Config()
    {

    }

    public static String getPropertyValue(String propertyName) throws IOException
    {
     URL url = new Config().getClass().getResource(configFileName);


        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputline = "";
        while ((inputline = br.readLine()) != null)
        {
           if (inputline.contains(propertyName))
           {
            int index = inputline.indexOf(propertyName);
            index += propertyName.length() + 1;

            return inputline.substring(index, inputline.length());
           }
        }
     return null;
    }
}


I want to be able to config file which includes the database connection string that is to be located at the same level as the jar. How could i achieve this? or is there a different approach to what i want?

I have a DB handler class which at the moment has the connection just hardcoded in.

+1  A: 

There are three options that I can immediately think of. The first is to find out the name of the jar file, which could be done using some tricky checking of the URL returned by a call to Class.getResource(), then work out the fully-qualified config filename.

The second option would be to put the directory itself in the classpath, and ask for the resource as an InputStream using Class.getResourceAsStream() or ClassLoader.getResourceAsStream().

Finally, you could pass in the name of the config file in some other way, e.g. via a system property, and load the file based on that.

Personally I like the second option - apart from anything else, it means that you could potentially ship the config file in the jar at a later date, if you wanted to - or move the config file to another directory and just change the classpath. The latter would also be easy if you specified the config filename directly as a system property: that would be my second choice.

Jon Skeet
Downvoters: please leave comments to explain why you disagree.
Jon Skeet
+2  A: 

This code is a start for you...

private static URI getJarURI()
    throws URISyntaxException
{
    final ProtectionDomain domain;
    final CodeSource       source;
    final URL              url;
    final URI              uri;

    domain = Main.class.getProtectionDomain();
    source = domain.getCodeSource();
    url    = source.getLocation();
    uri    = url.toURI();

    return (uri);
}

That gets the URI of the Jar file that you are running from.

TofuBeer
A: 

If by "the same level as the JAR" you mean that the properties file is in the same directory as the JAR file, the extremely easy way to accomplish what you're trying is:

public class MyMain {
   Properties props;
   MyMain(Properties props) {
      this.props = props;
   }

   public static void main(String[] args)
      throws Exception
   {
      File f = new File("just_the_name.properties");
      FileInputStream fis = new FileInputStream(f);
      Properties props = new Properties();
      props.load(fis);
      // Now, you'll have your properties loaded from "just_the_name.properties"
      MyMain mm = new MyMain(props);
      // ... and do whatever you need to do ... 
   }

I will also suggest you to use a class containing all your properties stored in individual class members so you will use them easily.

If your properties file reside inside the JAR file, you can go as the previous posts suggested.

Hope it helps.

Pablo Santa Cruz