I'm trying to access a file in my project. But getResource method returns null.
This is how my project looks like:
Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null
And how project folder in eclipse workspace looks like:
Why? I want to access files in my assets folder?
Edit
I created a jar file and this is content of the jar:
Solved
First of all, I've a lot of image files so I want to organize all them in a folder. I put the assets folder in src directory and finally I was able to access the files.
this.getClass().getClassLoader().getResource("assets/xxx.png");
There are lot of ways to add a resource to the jar file, you can put it in src, add as a resource if you use maven, ant etc... If you able to bundle whole directory then you should be able to use your original piece of code.
With the current structure you can use following piece of code.
Thread.currentThread().getContextClassLoader().getResource("/xxx.png").
Try using / prefixing.
Thread.currentThread().getContextClassLoader().getResourceAsStream("/xxx.png")
For someone struggling as me. For Maven just run mvn clean install.
After that Thread.currentThread().getContextClassLoader().getResource() should work.
Is there a reason you're using the the class loader of the current class? Something like this.getClass().getClassLoader().getResource("/xxx.png") should be more reliable.
Use the following code, it should work.
YOUR_CLASS_HERE.class.getClass().getResource( "/xxx.png" );
e.g.
Signin.class.getClass().getResource( "/xxx.png" );
Either Approach will work. its just Filepath issue.
Your Jar Structure shows no "asset" Folder
xxx.png file is directly in Jar File.
Try to remove "assets" from below line of code.
Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null
Also, if you want to use "assets" folder in ur classpath, please ensure that your jar contains "assets" folder.
Related
in my maven project, the resources folder look like this:
resources
images
TestImage.png
If I call
System.out.println(MainClass.class.getClassLoader().getResource("images"));
I get "file:/C:/Users/XYZXYZ/IdeaProjects/projectName/core/target/classes/images/"
If I add the file name like this:
System.out.println(MainClass.class.getClassLoader().getResource("images/TestImage.png"));
I get "null". How is this possible? What happens here?
Edit: I noticed that it works (does not give null anymore), as soon as I delete my module-info.java
Problem: I need my module-info.java. Any tips?
The following line of code:
SoyFileSet sfs = sfsBuilder.add(this.getClass().getResource("templates/mail.soy")).build();
works when run outside of a Jar but fails with a null pointer exception. Similar posts indicate that I should try getResourceAsStream but the add method wants a URL. So I think I have to convert the Stream to a URL but it is not clear to me exactly how to proceed.
sfsBuilder.add() also allows you to add content as CharSequence.
So, you can read the file yourself and add it to the builder.
Something like:
SoyFileSet sfs = SoyFileSet.builder().add(IOUtils.toString(this.getClass().getResourceAsStream("templates/mail.soy")), "file/path/for/messages").build();
IOUtils is from apache's commons-io project
This finally worked as follows:
{SoyFileSet sfs = sfsBuilder.add(this.getClass().getResource("/resources/templates/email.soy")).build();}
Not sure if this matters but in Eclipse the Runnable Jar was created using Package Required Libraries...
By the code you have posted, I guess templates/mail.soy is a file which belongs to your project, so it should be properly packed into the jar. However, the path should begin with a slash: /templates/mail.soy will match the following jar structure:
META-INF/
templates/
mail.soy
mypackage/
MyProgram.class
All, Forgive me I am still learning the Java development right row.
Say we have the structure of web project like below.
The src and config is under the Java Resources folder of web project.
src
...
|- a.b.c.package
|-test.java
...
config
|-1.xml
|-2.xml
...
configfolder
|-1.properties
|-2.properties
testfolder
|-test.properties
I want to know if I use the dom4j. How to read the xml file(1.xml) within the test.java. Thanks.
My Experiment
In the test.java. I found I can succeed to use the class.getClassLoader().getResourceAsStream("test.properties") to load test.properties in any folders or sub folder of src.
Does it mean getResourceAsStream can search the specified file in any of the folders of project recursively? I just can't understand it how it works. Thanks.
getResourceAsStream() method searches for resources with classpath as root. I suppose in your case 'testfolder' is source folder. I you would have your 1.properties in 'testfolder/mypath/1.properties' then you need to specify getResourceAsStream("mypath/1.properties")
Yesterday, I had a problem because I couldn't manage to open a xml file (it owuld give me a FileNotFoundException) located in the ressources folder of my .jar file, which I managed to open on eclipse using the following lines of code. You can see my old problem here. This was my code with the problem :
File xmlFile = new File("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(xmlFile);
Someone told me it that one way was to use getClassLoader().getRessourceAsStream method to open a xml file in a .jar file that was exported
InputStream i= this.getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(i);
Unfortunately, that solution only works when I export my project into a .jar file, so if I want to go back debugging my program, I have to take the old code that would only works on eclipse.
My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?
Thank you
edit :
Thank you all, it works perfectly fine now
my problem was that I put my ressources folder like that :
+project
+src
+ressources
+emitter.xml
InputStream i= this.getClass().getClassLoader().getResourceAsStream("/ressources/emitter.xml");
The above should work in both cases (Note is is /resources/.... This is assuming say your directory structure is below:
MyProject
+src
+ressources
emitter.xml
Place the file alongside your source files, then you can use the getResourceAsStream() method in both cases. Don't forget to update the path (which should be the package name of your class, but with slashes instead of dots).
My question is: is there any better way to do this without having to
change my code if I want to export it or if I want to debug it?
Yes, use Maven. Maven will handle that and it hooks into Eclipse beautifully (NetBeans too!) What you do is place the resource in src/main/resources and then you can have Eclipse run the test goal of the Maven project or you can just run mvn test from the command line. Another advantage of using Maven here is that you can also have src/test/resources/emitter.xml which overrides the one in src/main with environment-specific test instructions and it won't affect your deployment.
InputStream i= getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
or
InputStream i= getClass().getResourceAsStream("/ressources/emitter.xml");
(note the absolute positioning)
both work when the class is in the same jar, on the same class path.
In the jar the names must be case sensitive, but as the jar already works. Ensure that the ressources directory is on the class path too, or copied to the target directory.
As "ressources" is probably configured yourself (not named "resources" as in English), you probably need to add it to the build somehow.
I am trying to set the URL of an image that is located in my resource folder in my src folder. It is always setting the URL to null. The image is there and everything is named correctly. What is the issue? Thanks!
My file structure:
sp1
src
resources
01.png
Here is my code where I am trying to set the URL for the picture:
this.setImagePath(getClass().getResource("/resources/01.png"));
And the setImagePath is doing this, I am sure this is not the problem, because I followed the execution and it is setting it equal to null.
public final void setImagePath(URL imagePath) {
this.imagePath = imagePath;
}
Solution
I had to clean the build and rebuild. It worked after that fix.
I had to clean the build and then rebuild. Works now.
You need to check that, however you're building, the image file gets copied over to wherever the class files are compiled to. I.e. if your class is com.foo.MyImageLoader and located at <build dir>/com/foo/MyImageLoader.class, then the image should be at <build dir>/resources/01.png for you to be able to load it like that.
make sure the resource file is under your class path. For example, check whether your module/project is compiled to xxx.jar and saved under folder "target"
I had this problem as well, when I run mvn install, there is NO jar file generated, that's why no resource found in jar file in target
The cause of no jar generated is caused by following config in pom.xml, remove it and rerun install would be fine.
<packaging>pom</packaging>
If using JDeveloper, make sure that you have the extension of the file that you want copied to the classes folder for use selected. Right click on the project and choose project properties to get to this screen.
I would guess the URL should be "01.png" not "/resources/01.png"