java - Path of the folder inside the jar file -


i have jar file langdetect.jar. has hierarchy shown in image

shows project structure inside jar

there class languagedetection @ com/langdetect package. need access path of profiles.sm folder above class while executing jar file.

thanks in advance.

jars nothing else zip files , java provides support handling those.

java 6 (and earlier)

you can open jar file zipfile , iterate on entries of it. each entry has full path name inside file, there no such thing relative path names. though have take care, entries - although being absolute in zip file - not start '/', if need this, have add it. following snippet path of class file. classname has end .class, i.e. languagedetection.class

string getpath(string jar, string classname) throws ioexception {     final zipfile zf = new zipfile(jar);     try {         (zipentry ze : collections.list(zf.entries())) {             final string path = ze.getname();             if (path.endswith(classname)) {                 final stringbuilder buf = new stringbuilder(path);                 buf.delete(path.lastindexof('/'), path.length()); //removes name of class path                 if (!path.startswith("/")) { //you may omit part if leading / not required                     buf.insert(0, '/');                 }                 return buf.tostring();             }         }     } {         zf.close();     }     return null; } 

java 7/8

you may open jar file using java7 filesystem support jar files. allows operate on jar file if normal filesystem. walk filetree until have found file , path it. following example uses java8 streams , lambdas, version java7 derived bit larger.

path jarfile = ...;  map<string, string> env = new hashmap<string, string>() {{         put("create", "false");     }};  try(filesystem zipfs = newfilesystem(uri.create("jar:" + jarfilefile.touri()), env)) {   optional<path> path = files.walk(zipfs.getpath("/"))                              .filter(p -> p.getfilename().tostring().startswith("languagedetection"))                              .map(path::getparent)                              .findfirst();   path.ifpresent(system.out::println); } 

your particular problem

the above solutions finding path inside jar or zip, may possibly not solution problem.

im not sure, whether understand problem correctly. far see it, you'd have access path inside classfolder purpose. problem is, class/resource lookup mechanism doesn't apply folders, files. concept close package, bound class.

so need concrete file accessed via getresource() method. example myclass.class.getresource(/path/to/resource.txt).

if resources located in profiles.sm folder relative class , package, i.e. in /com/languagedetect/profile.sm/ build path reference class, example class languagedetection in package , derive absolute path profiles.sm path:

string basepath = "/" + languagedetection.class.getpackage().getname().replaceall("\\.", "/") + "/profiles.sm/"; url resource = languagedetection.class.getresource(basepath + "myresource.txt"); 

if there 1 profiles.sm in root of jar, go

string basepath = "/profiles.sm/"; url resource = languagedetection.class.getresource(basepath + "myresource.txt"); 

if have multiple jars resource in /profiles.sm, gain access of via classloader , extract jar file url of class

for(url u : collections.list(languagedetection.class.getclassloader().getresources("/profiles.sm/yourresource"))){         system.out.println(u);     } 

in case it's not possible without accessing zip/jar file browse contents of path or folder because java not support browsing classes or resources inside package/folder in classpath. may use reflections lib or extend classloader example above additionally reading content of detected jars using zip example above.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -