Pregunta

I found the class path of my running class by:

MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

For which I got below path as my result:

\Users\vanburen\main_dev\vob2\SD\WebContent\WEB-INF\classes

I have to read an XML file in location:

\Users\vanburen\main_dev\vob2\SD\rulesXML\rules.XML

Can someone help me how do I move three folders up? I've tried using

getAbsoluteFile().getParentFile()

but that seems to be redundant. Is there a better way of doing this?

¿Fue útil?

Solución 2

Since I haven't found a better way to traverse up the folders other than using getParentFile(), I've added the file I want to read to my classpath and got its location by

URL url = Thread.currentThread().getContextClassLoader().getResource(RULES_XML_NAME); File fXmlFile = new File(url.getPath());

And read the file by using, File fXmlFile = new File(url.getPath());

Otros consejos

I'm not sure why you have getAbsoluteFile(), but getParentFile() is by far the easiest and most accepted method of navigating up the folder tree.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top