How to read a resource file in Java standalone applications -
i creating java application in need read templates not able to. because not able locate files proper path.
the code structure follows:
the template reading class file in src/main/java/com.prototype.main while, templates residing in src/main/java/templates
which means, when create jar 3 folders 3 folders: com templates meta-inf
now class under package how can access templates? have tried following:
- new file("/") in case of web application have picked root directory of application in stand alone applications user directory of system
- system.getproperty("user.dir") gives project directory
- new app().getclass().getname() shows package name separated dot
so if want read template file /templates/some.xml, how entire path of file?
thanks in advance.
an embedded resource not file
, resource.
you can read these resources using class#getresource
or class#getresourceasinputstream
depending on needs.
for example...
url url = getclass().getresource("/templates/some.xml");
will return url
reference named resource.
Comments
Post a Comment