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.
Related
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
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.
I am working on a Java project (Eclipse IDE) which is using XWT files for UI purposes (which is basically an xml file). I need to dynamically edit one of those file during execution of the programm.
However, the project will not be executed from a definite directory and I need to get a relative path to the file.
So, say that my file is located under:
com.foo.goo.src.com.foo.goo.toto
Which translate on my disk by:
C:\\com.foo.goo\src\com\foo\goo\toto\file.xwt
But eclipse working directory is "C:\Eclipse\Workspace"
Is there a way to open this file ?
Thanks
Just use Paths class and the get method, like so:
Paths.get("C:\\file.txt").toFile()
Problem solved using FileResolve(URL url) :)
(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...
I'm making a simple game in Java, and trying to load a sprite from the jar file. I'm using the following code to load a sprite:
spriteURL = getClass().getResource("/res/sprites/sprite_fr1.png");
And if I export a jar file and unzip it, the following folders exist:
/res/sprites/sprite_fr1.png
However, when I try and load the image, I catch a NullPointerException, even though, as far as I can tell, the path exists?
What am I doing wrong here?
EDIT: Screenshot below.
All sprites are in the folder 'sprites' as PNG files. I'm calling the method from jeu, not jeu/canvases.
Regards,
Ben.
Check classpath of your project in your IDE. For example in eclipse there is configuration for each folder (Properties->JAva Build Path->Source Tab). It may be set to exclude png files.