Java get file path from class path




















Works like a charm. The provided one liner is all you need. Use the stream parsing from other examples if you don't know how to get a string from the stream. I had a bit of trouble figuring out exactly what to do with the resource variable as well. I've edited the answer with a bit more detail — DavidZemon.

I was already using Spring and trying the "pure java" way. It was killing me, the differences between getResource, getResourceAsStream, etc, with no good working examples. This is a perfect example of encapsulation, so I don't have to care. Be careful, if you package your project into a jar you should use an InputStream. If you use a File it work in your IDE but will fail if you test it from the jar.

If you really need a File try with stackoverflow. Show 1 more comment. Charset; import java. Files; import java. To read into a single String try. Best solution for me, as it doesn't need any dependencies, like Spring or Commons IO. This will fail if your resource file is inside a jar, e. In that case you'll need to use something like Spring 's StreamUtils.

A relative path might work better in this case. Chaitanya: Can you run the example from John Skeet's answer? Here are a few good examples for getResourceAsStream — drorw. Pavel Pavel 3, 25 25 silver badges 31 31 bronze badges. What about closing the inputstream? The stream will be closed automatically. It is a feature of Java 7 "Try with resources" docs.

Only if it is inside the try statement, which is not the case here. And then what? That information is no use by itself.

This information was perfect. I was only missing getPath! Patrick This answer does not provide the 'class absolute path'. It provides a URL. Not at all the same thing. Somehow the best answer doesn't work for me. I need to use a slightly different code instead.

This helped me on Android as well where a class was loaded by the application loader, but a key that it needed was lazy loaded in the UI thread. You need to provide information on why the best answer does not work for you e. The best answer said clearly that 1 the directory needs to be on classpath, 2 you need to request from a class loaded by the same class loader.

What actual application feature are you trying to implement? Serialization of settings, a 'self uninstaller'..? AndrewThompson : actually the code is using some files from the Directory. But if I use ".. By getting path at runtime, this problem is solved. Please tell if there is any easy way to prevent that. Show 2 more comments. Active Oldest Votes. Use System. Improve this answer. I don't see how this answer the question to be honest.

The system property 'java. The OP is asking for the path to the running main class working directory. The working directory is not necessarily where the class files are ran from. The example the OP gave proves this. While this answer does include all the locations of the project components, he shows how you can separate them into their elements.

The question evolved after I answered it. Thus my answer did not match anymore. Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 05 Feb, String line;.

Attention reader! Path for a classpath resource Ask Question. Asked 8 years, 9 months ago. Active 24 days ago. Viewed k times. Improve this question. JuanMoreno 1, 1 1 gold badge 17 17 silver badges 27 27 bronze badges. Louis Wasserman Louis Wasserman k 24 24 gold badges silver badges bronze badges.

Well, taking the long path pun intended , you have Paths. You might be able to chain those together. Add a comment. Active Oldest Votes. This one works for me: return Paths. Improve this answer. VGR if resources in. It doesn't work at all when Spring isn't being used. If your application doesn't rely on the system classloader, it should be Thread.

Holger Holger k 34 34 gold badges silver badges bronze badges. This solution works great!



0コメント

  • 1000 / 1000