loading image from a project's folder (not bin) in java - java

I imported some images into my project javaproject under the folder name images. Now, since earlier the paths were referring to C: drive, I figured I've to change the path. I tried using
this.getClass().getClassLoader().getResourceAsStream("images\\img1.jpg");
I'm not sure what happened there. There was no error yielded whatsoever, but the image is not displaying. I'm fairly new to programming in general so I can't think alternate methods.
How do I re-direct it to the main directory of my project and into my images folder?
Also, I cant have a strict location like:
c:/users/me/java/etc because if this JAR is ever on different computer it won't work.
Any help is appreciated. Thanks!

if you want to use the above approach then all resources files have to be stored under src/main/resources

Related

Java getResourceAsStream Returns NullPointerException?

This will no doubt be marked as a duplicate but I figured I'd try anyway. I've looked at all the related questions that have been asked, and I've searched online, and I can't seem to find the solution to this issue.
I am trying to use the Java FX diffuseMap to texture a Box. However, when using
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("/Eternity/Images/ice.png")));
b.setMaterial(material);
I get a NullPointerException;
java.lang.NullPointerException: Input stream must not be null
I have tried messing with the filename, paths, different ways of accomplishing the same task (all of which still involved InputStreams) and even copying and pasting the actual file path (using Intellij's Copy File Path button) but I can't seem to solve this issue. It just doesn't seem to have any effect on the error.
Here's a look at the project hierarchy, barring the main project folder.
Any and all help would be greatly appreciated!
Class.getResourceAsStream() uses the class loader, to load a resource from the classpath. So, for your code to work, the directory containing the Eternity directory should be in the classpath. Since it's not, the resource can't be found by the class loader, and null is returned, as documented.
So, either change the classpath of your running program, or move the images to the source folder, so that your IDE (and, hopefully, your build/packaging tool), copies the images to the directory where the class files are compiled. And then use the right path of course (i.e. the full package name, but with slashes instead of dots, and with a starting slash). If the image is in src/foo/bar/ice.png, the package is foo.bar, and the path to use is thus /foo/bar/ice.png.
Seems like you're using Intellij, just drag the images directory and drop it in the src directory and rename the path to
... .getResourceAsStream("/Images/ice.png")));
#JB Nizet did a good job of explaining why it doesn't work.
My solution is a temp-fix the right way would be to correctly configure a resource directory and then put your resources there. I suggest you go through Intellij Modules

In Java where do I put my sprite files to make this open source code work?

I am a beginner and I am trying to learn by messing around with some open source game code.
I was setting it all up in Eclipse but I don't know where to put these sprite gif files.
In the code I found this:
URL url = this.getClass().getClassLoader().getResource(ref);
And when I put all the class files under a java project together and tried to run I got this error message:
Can't find ref: sprites/ship.gif
Of course the code came with sprites including ship.gif. I just don't know where to put it. I tried making a folder under the java project called sprites and putting it in there.
I don't have a res folder.
The this.getClass().getClassLoader().getResource method look for relative path of the data from the package of the class.
Assuming your class is com.my.package.MyClass, you usually have your project organized containing at least in your case:
com/my/package/MyClass.java
com/my/package/sprites/ship.gif
Most of the time, you certainly have a resources or a images folder for your java project.
You can only load those resources if they are on the classpath. Try to add the sprites folder as a source folder on your Eclipse build path and try again.
Put the .gif in the jar.
This can be achieved by creating dedicated folder you add in the sources of eclipse. Often this folder is called "resources".
Be ware that in some cases a "/" is required at the begining of ref.
Thanks everyone.
How I fixed my problem:
When I downloaded the source I opened folders and went to the class files and copied them to my project and then I was trying to do the same thing with the sprites. What I didn't realize is that if I just drop the two main folders "org" and "sprites" into the source file of my project then it all works on its own. These two folders were the first things I had after downloading this open source code.

Eclipse LibGDX folder sharing

(4/15/2014 Still no working answer to the question)
I used gdx-setup-ui.jar to create my Android/Desktop/HTML5 program which I imported into Eclipse.
myprogram
myprogram-android
myprogram-desktop
myprogram-html
My program runs fine on desktop and android, but when I run it as html I get an error if all of my classes are not in the same myprogram>src folder, if I put it in a sub folder in src then the html5 does not seem to access the class. If I take my classes out of the folder and put them in the root of myprogram>src they work. How can I fix this?
I also notice that when I do a symbolic link to the asset folder manually(without gdx-setup-ui.jar) my Java application will not find the pictures etc unless I physically copy the asset folder twice. I made sure the folder path was exactly the same and it still wouldn't detect it unless I made a second copy of the pictures. Strangely the gdx-setup-ui.jar does this symbolic copy and yet it works for some reason.
This tutorial explains how to do it in Eclipse. And for completeness sake, here is how to do it using ant and IntelliJ. I hope this was useful. I will try the Eclipse one now...

Runnable JARs missing Images/Files (Resources)

When I export my code as runnable JAR from eclipse all the files that I've set it to grab such as button images and other files are missing even though they are actually in the JAR. I've added getClass().getResource in front of the files but then when I try to run the JAR nothing even happens, any suggestions?
Seems like you not putting your stuff in the right sense. In order to make it work, follow these steps :
Right-Click your Project in Project Explorer Tree.
Go to New -> Source Folder and then provide any Name to the Source Folder.
Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
Name this New Folder as images and copy your images to this Folder.
Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
Now in order to access, say any image, you will use.
getClass().getResource("/images/yourImageName.extension");
which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.
The path needs to be right for the resource.
For "foo.gif" being at the root of the jar, you must refer to it using "/foo.gif".
If the program works correctly after a complete clean and rebuild, but fails as a jar, you most likely do not have the files included in the jar.
Try to put the folders in the jar the same way that you got them in the program. Put in the same resources in the same places that you have them in the project. The jar will reference to them the same way as in your compiler did.
You need to get the images using stream like this -
this.class.getClassLoader().getResourceAsStream("test.jpg") and make sure the images are present in the jar which you are referencing.
As nIcE cOw said, you just need to create a Source Folder in you Project Explorer Tree.
All the files inside that folder will be in the root project folder.
To refer to them, you must write your projects name slash the file name as it:
getClass().getResource("ProjectName/image.extension");
I hope this helps!

Eclipse IDE project directory structure help [Java]

I have been learning Java and have had no problem with projects in which multiple .java files were in the same Default Package.
I am now trying to separate code and create folders for images, but everything I have tried has failed.
How do I properly add folders for images and other classes, and properly set the path to it?
All paths should be relative to the project, I know that much ;_;
Here's a link to a picture of my IDE and error message from program output:
http://img262.imageshack.us/img262/8415/directory.png
Thanks!!
First off, your Java code itself should go into a package, not the default package. Next, you may be able to refer to the image file by prepending src/ to your path:
"src/Textures/Crate.png"
But better would be to get the image as a resource, not as a file using the Class#getResourceAsStream.
Drag using the mouse your Textures folder to the LWJGL 6 project folder.
Try giving src/Textures/Crate.png as the path to the FileInputStream constructor.

Categories