java - Converting a Jar-URI into a nio.Path -
i'm having trouble coverting uri nio.path
in general case. given uri multiple schemas, wish create single nio.path
instance reflect uri.
//setup string jarembeddedfilepathstring = "jar:file:/c:/program%20files%20(x86)/oursoftware/ourjar_x86_1.0.68.220.jar!/com/our_company/javafxviewcode.fxml"; uri uri = uri.create(jarembeddedfilepathstring); //act path niopath = paths.get(uri); //assert --any of these acceptable assertthat(niopath).isequalto("c:/program files (x86)/oursoftware/ourjar_x86_1.0.68.220.jar/com/our_company/javafxviewcode.fxml"); //--or assertthat(niopath).isequalto("/com/our_company/javafxviewcode.fxml"); //--or assertthat(niopath).isequalto("ourjar_x86_1.0.68.220.jar!/com/our_company/javafxviewcode.fxml") //or pretty other interpretation of jar'd-uri-to-path reasonable person have.
this code throws filesystemnotfoundexception
on paths.get()
call.
the actual reason conversion ask resulting path things regarding package location , file name --so in other words, long resulting path object preserves ...com/our_company/javafxviewcode.fxml
portion, still convenient use nio path object.
most of information used debugging, not impossible me retrofit our code avoid use of paths in particular instance , instead use uri's or strings, involve bunch of retooling methods conveniently provided nio.path
object.
i've started digging the file system provider api , have been confronted more complexity wish deal such small thing. there simple way convert class-loader provided uri path object corresponding os-understandable traversal in case of uri pointing non-jar file, , not-os-understandable-but-still-useful traversal in case path point resource inside jar (or matter zip or tarball)?
thanks help
a java path
belongs filesystem
. file system implemented filesystemprovider
.
java comes 2 file system providers: 1 operating system (e.g. windowsfilesystemprovider
), , 1 zip files (zipfilesystemprovider
). these internal , should not accessed directly.
to path
file inside jar file, need (create) filesystem
content of jar file. can path
file in file system.
first, you'll need parse jar url, best done using jarurlconnection
:
url jarentryurl = new url("jar:file:/c:/program%20files%20(x86)/oursoftware/ourjar_x86_1.0.68.220.jar!/com/our_company/javafxviewcode.fxml"); jarurlconnection jarentryconn = (jarurlconnection) jarentryurl.openconnection(); url jarfileurl = jarentryconn.getjarfileurl(); // file:/c:/program%20files%20(x86)/oursoftware/ourjar_x86_1.0.68.220.jar string entryname = jarentryconn.getentryname(); // com/our_company/javafxviewcode.fxml
once have those, can create filesystem
, path
jar'd file. remember filesystem
open resource , needs closed when done it:
try (filesystem jarfilesystem = filesystems.newfilesystem(jarpath, null)) { path entrypath = jarfilesystem.getpath(entryname); system.out.println("entrypath: " + entrypath); // com/our_company/javafxviewcode.fxml system.out.println("parent: " + entrypath.getparent()); // com/our_company }
Comments
Post a Comment