Questions tagged [getresource]

283 questions
230
votes
9 answers

What is the difference between Class.getResource() and ClassLoader.getResource()?

I wonder what the difference is between Class.getResource() and ClassLoader.getResource()? edit: I especially want to know if any caching is involved on file/directory level. As in "are directory listings cached in the Class version?" AFAIK the…
oligofren
  • 20,744
  • 16
  • 93
  • 180
150
votes
17 answers

How to list the files inside a JAR file?

I have this code which reads all the files from a directory. File textFolder = new File("text_directory"); File [] texFiles = textFolder.listFiles( new FileFilter() { public boolean accept( File file ) { return…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
94
votes
5 answers

How to use ClassLoader.getResources() correctly?

How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF "directory": Imagine something like getClass().getClassLoader().getResources("META-INF") Unfortunately, this does…
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
75
votes
4 answers

Get a resource using getResource()

I need to get a resource image file in a java project. What I'm doing is: URL url = TestGameTable.class.getClass(). getClassLoader().getResource("unibo.lsb.res/dice.jpg"); The directory structure is the following: unibo/ lsb/ res/ …
lbedogni
  • 7,917
  • 8
  • 30
  • 51
53
votes
15 answers

Junit + getResourceAsStream Returning Null

Not sure how this is possible. I re-read up on getResourceAsStream and it's always returning null. InputStream source = this.getClass().getResourceAsStream("test.xml"); Right next to test.java in the Finder (using OS X and Eclipse) is test.xml I…
bobber205
  • 12,948
  • 27
  • 74
  • 100
45
votes
6 answers

How to get getclass().getResource() from a static context?

I have a function where I am trying to load a file to a URL object, because the example project said so. public class SecureFTP { public static void main(String[] args) throws IOException , ClassNotFoundException, SQLException , JSchException,…
roymustang86
  • 8,054
  • 22
  • 70
  • 101
39
votes
13 answers

Copy directory from a jar file

I have recently developed an application and created the jar file. One of my classes creates an output directory, populating it with files from its resource. My code is something like this: // Copy files from dir "template" in this class resource to…
Macarse
  • 91,829
  • 44
  • 175
  • 230
24
votes
3 answers

getResource puts a leading / before the disk name using java 1.7 windows 7

The following gives a leading slash before the disk name. How can I avoid that? String pngpath = getClass().getResource("/resources/image.png").getPath(); System.out.println("pngpath = "+pngpath); Gives: pngpath =…
Jonathan Grimsdale
  • 241
  • 1
  • 2
  • 3
14
votes
3 answers

getResourceAsStream from JUnit

I am creating a JUnit TestCase for a project which needs to load a configuration file during initialization. This configuration file is inside the project in src/main/resources/config folder and during the build maven places it into /config folder,…
Carles Sala
  • 1,989
  • 1
  • 16
  • 34
14
votes
2 answers

Class.getResource() returns null

I am trying to display pictures on the JPanel but I keep getting the error : java.lang.IllegalArgumentException: input == null! I don't understand what is happening. Here is the code I am using: public void actionPerformed(ActionEvent e) { try…
user2201158
  • 153
  • 1
  • 2
  • 8
12
votes
2 answers

Access a resource outside a jar from the jar

I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar. my-dir: tester.jar test.jpg I tried different things including the following, but every time the input stream is null: [1] String…
mdp
  • 171
  • 1
  • 1
  • 8
11
votes
1 answer

Java 9.0 | ClassLoader::getResourceAsStream: NullPointerException

This piece of code down below, where I take my file from folder which is inside the "/resource" folder, works fine for me in Java 8: //e.g fileName = "folder0/file1.extension2" ClassLoader classLoader =…
voismager
  • 433
  • 4
  • 19
11
votes
2 answers

How to use getResources() on a Adapter.java

I want to add icons on my TabView, but I am stuck because I can't use getResources() in a custom TabAdapter. I already try to use getAplicationContext().getResources, getActivity.getResources(), but i am still seeing the message "cannot resolve…
GLAPPs Mobile
  • 99
  • 1
  • 1
  • 12
9
votes
5 answers

Cannot I do a getResources.getString dynamically?

I'm trying to fill a Spinner with the values of some Strings of string.xml. I'm trying to do like that: final List list = new ArrayList(); for (int i = 0; i < 86; i++) { String resource = "R.string." + ntpServers[i][0]; …
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
9
votes
2 answers

Get resources from another apk

I have been struggling with this issue all day and have had no success. I am basically trying to get an image resource from another apk. So if com.example.app has an image called image1.png in the res folder, i want com.example2.app to be able to…
user1190019
  • 565
  • 1
  • 5
  • 17
1
2 3
18 19