Get url from jar file
Spring has the Resource class which allows you to load classpath resources quite nicely. File; import java. FileReader; import java. InputStreamReader; import java. Reader; import java. URI; import java. URL; import java. Enumeration; import java. ZipEntry; import java. Think of namespace like a file extension but it is the start of the file not the end.
Look for all resources that match the location we are looking for. Always use this if available. RickHigh RickHigh 1, 19 19 silver badges 15 15 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Connect and share knowledge within a single location that is structured and easy to search.
I'm trying to access an XML file within a jar file, from a separate jar that's running as a desktop application. As a point of reference, I have no trouble reading image resources from the same jar, passing the URL to an ImageIcon constructor. This seems to indicate that the method I'm using to get the URL is correct. You don't say if this is a desktop or web app. I would use the getResourceAsStream method from an appropriate ClassLoader if it's a desktop or the Context if it's a web app.
It looks as if you are using the URL. In any case, the string is not a file path. The parse method accepts an InputSource, so there was no reason to even use a FileReader. Changing the last line of the code above to. I'd like to point out that one issues is what if the same resources are in multiple jar files.
I have run into this same issue several times before. I was hoping in JDK 7 that someone would write a classpath filesystem, but alas not yet.
I wrote a little prototype to solve this very problem of reading resources form multiple jar files. The prototype does not handle every edge case, but it does handle looking for resources in directories that are in the jar files. I have used Stack Overflow for quite sometime.
This is the second answer that I remember answering a question so forgive me if I go too long it is my nature. You can see a fuller example here with the sample output. Here's a sample code on how to read a file properly inside a jar file in this case, the current executing jar file. Then change the filePath to the path of the file you want to use inside the jar file. Outside of your technique, why not use the standard Java JarFile class to get the references you want?
From there most of your problems should go away. If you use resources extensively, you might consider using Commons VFS. Returns: the URL for the Jar file for this connection. Returns: the entry name for this connection, if any. Returns: the JAR file for this connection. Returns: the manifest object corresponding to the JAR file object for this connection.
Returns: the main Attributes for the JAR file for this connection. Lucifer Add a comment. Active Oldest Votes. I am going with exactly this solution, although I wish there was a way to do this without opening the jar or parsing directly, of course ; seems like URL needs to understand this weird!
It just parses the URL on your behalf. Marcono This will throw an exception if the URI points to something inside a jar, thus having exclamation marks , as is the case for OP. DmitryAvtonomov, you are right, my suggestion to use Paths. I removed that comment. Not sure of any exact method that will give you what you want, but this should get you close: import static org. URL; import org. Very close, but the usage of split is too much low-level parsing for me.
0コメント