5

I’m creating an executable JAR that will read in a set of properties at runtime from a file. The directory structure will be something like:

/some/dirs/executable.jar /some/dirs/executable.properties 

Is there a way of setting the property loader class in the executable.jar file to load the properties from the directory that the jar is in, rather than hard-coding the directory.

I don't want to put the properties in the jar itself as the properties file needs to be configurable.

1

2 Answers 2

13

Why not just pass the properties file as an argument to your main method? That way you can load the properties as follows:

public static void main(String[] args) throws IOException { Properties props = new Properties(); props.load(new BufferedReader(new FileReader(args[0]))); System.setProperties(props); } 

The alternative: If you want to get the current directory of your jar file you need to do something nasty like:

CodeSource codeSource = MyClass.class.getProtectionDomain().getCodeSource(); File jarFile = new File(codeSource.getLocation().toURI().getPath()); File jarDir = jarFile.getParentFile(); if (jarDir != null && jarDir.isDirectory()) { File propFile = new File(jarDir, "myFile.properties"); } 

... where MyClass is a class from within your jar file. It's not something I'd recommend though - What if your app has multiple MyClass instances on the classpath in different jar files (each jar in a different directory)? i.e. You can never really guarantee that MyClass was loaded from the jar you think it was.

Sign up to request clarification or add additional context in comments.

Comments

0

public static void loadJarCongFile(Class Utilclass ) { try{ String path= Utilclass.getResource("").getPath(); path=path.substring(6,path.length()-1); path=path.split("!")[0]; System.out.println(path); JarFile jarFile = new JarFile(path); final Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { final JarEntry entry = entries.nextElement(); if (entry.getName().contains(".properties")) { System.out.println("Jar File Property File: " + entry.getName()); JarEntry fileEntry = jarFile.getJarEntry(entry.getName()); InputStream input = jarFile.getInputStream(fileEntry); setSystemvariable(input); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { System.out.println("Jar file"+line); } reader.close(); } } } catch (Exception e) { System.out.println("Jar file reading Error"); } } public static void setSystemvariable(InputStream input) { Properties tmp1 = new Properties(); try { tmp1.load(input); for (Object element : tmp1.keySet()) { System.setProperty(element.toString().trim(), tmp1.getProperty(element.toString().trim()).trim()); } } catch (IOException e) { System.out.println("setSystemvariable method failure"); } }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.