I have setup a project in eclipse, which has the default src folder and a newly created source folder called rsc. Inside the rsc folder, there is an audio file called sound.wav, which I am accessing with the following code:
InputStream input = this.getClass().getClassLoader().getResourceAsStream("sound.wav");
And this works just fine, when I put it into a Clip for example I can play the sound. However, if I move the file into the src folder, the file is no longer found and the method returns null. I did try different permutations of the url like the following, but none worked.
"/sound.wav"
"src/sound.wav"
"/src/sound.wav"
When looking at the project properties under the source tab, both folders are marked as source folders and have the same settings. So the question remains, why can java only find the resource inside the rsc folder, but not inside the src folder?
Related
I've made a function(Java) that is supposed to read bytes from a file and print them to the console:
public void loadPixels(int size){
ClassLoader cl = this.getClass().getClassLoader();
pixels = new byte[size];
try{
InputStream stream = cl.getResource("res/" + fileName).openStream();
stream.read(pixels);
System.out.println(pixels.toString());
stream.close();
}catch(Exception e){
e.printStackTrace();
}
}
The problem is, I'm getting a NullPointerException on the line
InputStream stream = cl.getResource("res/" + fileName).openStream();
For the file I'm trying to open, the name is "font.spt", which is also the value held by fileName. This file is within the folder "res" in the project's root directory, and I'm currently using the Eclipse IDE.
Is my approach to the path for the file wrong, or is something else the issue?
To recap: fileName points to "font.spt", which is under the "res" folder in the bin directory.
EDIT: the "res" folder containing the .spt file is now under the "bin" for the project, rather than the root directory, but I still get the error. When running from the IDE or as an exported .jar, I still get the NullPointerException, where am I supposed to put these files? Can someone give me a screenshot or example?
By default, Eclipse will copy all non .java files it finds in the project's Source Locations to the Output Folder when it builds. So one option is to just place your resource files under the source folder along with your source code. However, a better option is to use a separate resources folder for non-Java files and declare that as an additional Source Location (via your project's Java Build Path properties). That's how Maven and Gradle projects are organized, too.
For example, you might have:
MyProject\
src\
java\
com\
....*.java
resources\
fonts\
font.spt
With both src\java\ and src\resources\ defined as Source Locations in the Build Path. Then your code to load the file would be like:
getResource("fonts/" + fileName)
Something to consider: use Gradle or Maven to manage your builds, both of which enforce/provide a project structure similar to this anyway.
I really need your help to solve my own problem. Now, I'm dealing with small code app. In that project folder contain some resource files (*.xlsx, *.png,...). I placed them in current folder with code file. I just wonder that when I run my code in netbean ide, it just worked find.
After I build code project, I get a jar file in "dist" directory. I run it. It open normally since app used JFrame as user interface. However, when I execute some function of that app, it showed me the error log. Here is the error message:
java.io.FileNotFoundException:
src\sample.xlsx (The system cannot find the path specified)
What's the matter out there?
Here is some pieces of my code:
copyFile(new File("src\\sample.xlsx"),
new File(txtout.getText()+"\\sample.xlsx"));
Node: copyFile function is used for copy file from source to dest.
Here is my project folder structure in Netbean IDE:
Project Name
Source Pakage(src)
myClass.java, sample.xlsx, etc
First, never reference src directly, the directory will not exist once the program is built. Second, you can not access resources which have been embedded within in the application context via a File reference, they simply no longer exist on the file system.
Instead, you need to use Class#getResource or Class#getResourceAsStream
URL url = getClass().getResource("/sample.xlsx");
InputStream is = getClass().getResourceAsStream("/sample.xlsx");
// Don't forget to manage your streams appropriately...
Well you can create a folder named resources under the src folder put your resources in it and use them in your code by using getResourceAsStream() and getResource() methods that can access the embedded resources.Clean and Build will compile the code and embed the contents of the resources folder into the application’s .jar file.
Ways of Accessing resources :
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResourceAsStream(pathToImage );
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResource(pathToImage );
please refer the link information
I have a class in a package com.mwerner.utils that needs the path for an getResourceAsStream() call
the file it is supposed to load is located in a subfolder of the project root i.e.:
/src/com/mwerner/utils/myfile.java has to load
/res/file.xml
I tried stuff like
/res/file.xml
../res/file.xml
res/file.xml
What is the right one?
EDIT:
I am using Xstream to parse the XML into objects. The line of code in question is:
ObjectInputStream in = xstream.createObjectInputStream(Utils.class.getResourceAsStream("res/file.xml"));
I get an IOException with unknown source
Turns out Eclipse puts the entire content of the res folder in the root folder of the bin folder. Therefore the path is just very simply /file.xml. I tried putting it into a subfolder of res called xmls and the path then is /xmls/file.xml
I also went to the source tab of the java build path and added the res folder.
I'm struggling to reference a .dat file in my java project in Eclipse. My file structure looks like this:
I'm trying to reference 'GeoIPLite.dat' from my 'LookupCountry.java' file. When I say "Reference" I actually mean just getting a String object of the path and then sending that as a parameter to a class that is located under the 'Referenced Libraries' in a jar.
My code in LookupCountry.java is as follow:
try{
String dbFile = "../resources/GeoIPLite.dat"
// TRIED ALL THESE AS WELL:
//String dbFile = "/../resources/GeoIPLite.dat"
//String dbFile = "/resources/GeoIPLite.dat"
//String dbFile = "resources/GeoIPLite.dat"
//Some more code calling the jar file......
}catch(IOException e){
//Handles exception
}
So basically I get a IOException that gets thrown from the jar saying that the path to the .dat file is incorrect.
Any ideas??
Use the same package structure in your resource folder as you did in your src folder. Add your resource folder as a source folder in Eclipse (see screenshot). Place the .dat file, in the resource folder, relative to the class that is going to use it. You should see your resource files then along side your compiled classes.
In your code, reference the resource file like so:
this.getClass().getResource("foo.txt");
In Eclipse, for example, when you run a class, you configure it as to where the 'current folder' is. From the menu: Run > Run Configuration ... and select the one you are using.
Then click the 'Arguments' tab and check the working directory.
Your path should be relative to the location shown there. (In the clipped image it shows the default which is the base folder of the project itself.)
You would open the file as new File("resources/GeoIPLite.dat") with the default working directory.
I have created a project, inside the project there is a folder named Source Packages, which contains my packages.
I need images, and I want to copy them in a folder inside the project (or better inside the Source packages), so that when I create the .jar the image folder will be inside the jar file.
How can I do that in NetBean?
Edit: I still can't figure it out, this is the code:
Image star;
InputStream stream = getClass().getResourceAsStream("images/star.png");
star= ImageIO.read(stream);
It doesn't work, I get the error "IllegalArgumentException input==null"
the folder "images" is inside the folder of the project "Game", if I try with this code:
Image star;
InputStream stream = getClass().getResourceAsStream("images/star.png");
star= Toolkit.getDefaultToolkit().getImage("images/star.png");
it works, what am I doing wrong with the InputStream?
You can just copy/paste your images into your package or into the folder which is represented by the package. When you want to access the images:
InputStream stream = getClass().getResourceAsStream("/com/my/pkg/my_image.png");
See this SO question for some more examples:
how to add image from spectified package into label, frames etc
Edit: ClassLoader.getResourceAsStream(String) will find any resource that is located in the package you specify for the path. For example, if you want to get an image that resides in com.my.pkg.a from a class that resides in com.my.pkg.b:
// From com.my.pkg.b.MyClass
InputStream stream = getClass().getResourceAsStream("/com/my/pkg/a/my_image.png");
Notice that the path specified goes to package a. This will get find the image even though it resides in a different package. See the javadoc for ClassLoader.getResource(), which is what is used internally, for more detailed information.
create a folder in your root project folder ("resources" for instance)
in the project properties dialog, "Sources" page, click on the "Add
Folder..." button that is at the right of the "Source package
folders" table
Select your newly created folder, click OK
Change the label for your folder in the "Source package folders"
table ("Resource Packages" for instance)
The files in that folder will be included in the jar of your application.