I'll try to get back to that later. Also, if I didn't stress it properly earlier, with this approach the resource file that you're trying to read from must be in the same directory in the jar file as this class. This is inferred by the getClass. Also, I haven't looked at it in a while, but I think you can just call the is. Here's one more example of how to do this, this time using some code from a current Scala project:. Although the code shown is Scala, I think you can see that you can use this approach to read the file as a java.
You indeed have to use forward slashes, even on Windows. That implies that you cannot use File. This is the only solution I was able to get to work, and I was working on this for hours. Baz Anatoly Anatoly 3, 1 1 gold badge 13 13 silver badges 8 8 bronze badges. Anatoly Could you please share more datails on above — Kasun Siyambalapitiya. Shano Shano 2 2 silver badges 5 5 bronze badges. Lamberto Basti Lamberto Basti 1 1 gold badge 5 5 silver badges 22 22 bronze badges.
Maybe this method can be used for quick solution. Are you omitting some context? This gives me: java. NullPointerException: Attempt to invoke virtual method 'java. CodeSource java. I edited the answer and wrote the whole method which i used in a software — gbii.
But if it's in a jar then it's not a File Vilnis Krumins Vilnis Krumins 13 3 3 bronze badges. This Class function can get absolute file path by relative file path in. Nori Nori 9 9 silver badges 24 24 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. Stack Gives Back Anthon 74k 41 41 gold badges silver badges bronze badges.
Sergio Sergio 6 6 bronze badges. What this person did looks interesting Add a comment. Active Oldest Votes. Improve this answer. Gilles 'SO- stop being evil' Gilles 'SO- stop being evil' k gold badges silver badges bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Viewed k times. I am trying to get a path to a Resource but I have had no luck. Improve this question. I've got a class that I would like to work with both, a folder on the outside in case I want to change some parameter of the config file and a JAR that hides the implementation configuration files to the user like a distributable JAR to all people.
So the class just receives a PATH to a file the config file. Then you should probably have that class deal with an input stream, which you can get from either source. Yes, I know. But it would have been more clear and clean the other way. But thx anyway. Are you trying to do something like option 4 in this question?
Show 3 more comments. Active Oldest Votes. Improve this answer. Neal Maloney Neal Maloney 1, 8 8 silver badges 5 5 bronze badges. Add a comment. When loading a resource make sure you notice the difference between: getClass. Tombart Tombart I do understand that there might be some security risks related to this approach, so application owners need to be aware of that. How can this provide a solution to the problem?
That was meant just for the case of images, sorry it wasn't very clear. The approach should be platform independent, though it's a bit hacky way. LucasA Could you clarify the risks you mentioned?
Maicon Mauricio 1, 1 1 gold badge 7 7 silver badges 22 22 bronze badges. Manojkumar Khotele Manojkumar Khotele 11 11 silver badges 23 23 bronze badges. Not everything can utilize a URI that begins with jar. Case in point JavaFX Media.
0コメント