java - How to find absolute path from a relative path in file system -
a java class readfile1 in testa project. , class testread in project testb. want read class readfile1 in testread class. have relative path, how can read that.
example
my class present in d:\eix-new-source\source\message-kernel\src\main\java\com\eix\transform\parser\messagetransformer.java
message-kernel project in source folder.
and doing test in d:\eix-new-source\source\msg-test\src\com\corejava\rnd\relativepath.java
when run program got result d:\eix-new-source\source\message-kernel\target\classes
but expecting result should d:\eix-new-source\source\message-kernel\src\main\java\com\eix\transform\parser\messagetransformer.java
try below method getting absolute path of class file , can append remaining path relative file path actual absolute path of file.
string getabsolutepath(){ java.security.protectiondomain pd = yourclassname.class.getprotectiondomain(); if ( pd == null ) return null; java.security.codesource cs = pd.getcodesource(); if ( cs == null ) return null; java.net.url url = cs.getlocation(); if ( url == null ) return null; java.io.file f = new file( url.getfile() ); if (f == null) return null; return f.getabsolutepath(); }
Comments
Post a Comment