Find the conf file no matter how the application was launched

I am new to java.

I have a directory structure

  product/
        conf/
        classes/com/../.. 

conf/contains the configuration file, and in classes/I have my application.

How can I ensure from internal java code that I can find the file in conf/, even though I execute it (for example, from eclipse, from different directories, from crontab, etc.).

PS

Files in conf/are not resources, as they must be edited by the user. Is there any way to find out where mine is .class, so I can use the relative path for this directory to access my directory (e.g. MY_CLASS_DIR /../../../../ conf)

+3
source share
4 answers

, .

, ,

java -DXY_HOME=/some/path/product ...

javacode :

String xyHome = System.getProperty ("XY_HOME") 

inifile , , conf-.

, , . , (, crontab,...)? CWD, - , , .

, , .

+2

conf . , :

YourClass.class.getClassLoader().getResource("conf/....");
+4

, . , , ,

. ( , )

product/       
    classes/com/../..
    classes/conf/some_conf.properties

Apache, URL-

URL urlOfFile = org.apache.commons.configuration.
ConfigurationUtils.locate("conf/some_conf.properties");

, ,

URL urlOfFile = <SomeClassFromClassesFolder>.class.
getClassLoader().getResource(resourceFile);

URL- , ,

InputStream stream = urlOfFile.openStream();

.

, :

http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_IO_CurrentWorkingDirectory

http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_Reflection_WheretheClassloadedfrom .

+1

, :

String str = new File("").getAbsolutePath()
0

All Articles