im fairly new to java and im making a pokemon style game for practice and i would like to be able to send the game to my friends.
here is the main problem: the game works fine in my netbeans IDE, but using the jar file in my dist folder does not work and throws a nullPointerException. i have narrowed down the problem. my game uses imageIcons and png/gif images that i have imported in my libraries. im getting access to them like this
Icon bckground = new javax.swing.ImageIcon(getClass().getResource("/pictures/BG.gif"));
i am unsure how to get the images into the lib folder for the program to find. i have tried copying the files straight into the lib folder and creating a folder for them called pictures; neither worked. right now the lib folder contains only a single jar from one of my other libraries. (that is the only jar file that i am importing to my libraries)
Pic of what it looks like in IDE
In my case, i did something like this.
final BufferedImage image = ImageIO.read(new File("something.jpg"));
I just transferred something.jpg into the dist folder, and it worked fine.
After clean-building your project, put the BG.gif into the dist folder, then run your jar file in dist folder. Now everything should be fine. When sending your game to your friends, you can encapsulte (hide, set read-only) your code (google encapsultaion java), then with the BG.gif being transferred into dist folder, archive the project. Then your friends only need to unzip it and find jar file in dist folder.
Hope this will help:)
Please put your picture (BG.gif) in the package (directory) where it is used as icon Icon bckground = new javax.swing.ImageIcon(getClass().getResource("BG.gif")); inside the jar file. You need to change the path of the file first getResource("BG.gif") and then create the jar file from IDE. If the jar does not contain the image, you can open the jar using using any unzip application (winrar etc.) and copy and paste the the image file in the directory where the class is present. Please let me know the outcome.
Related
im wring a simple program and my images load in fine in the project but as soon as i export it into a jar file none of them load. i have little to no experiance creating artifacts or jar files.
project structure:
project:
src:
myclass1, myclass2...
res:
image1.png image2.png...
im using Toolkit.getdefaultToolkit to load in images
in my class i am loading the images in the constructor by writing
myImage = toolkit.getImage("res/image1.png");
this works perfectly fine in the project. does not work in the jar
i have also tried
myImage = toolKit.getImage("image1.png");
which does not work in the project or while opening the jar
i know toolkit is not the best way to go about loading images but i would like to know how i can fix this issue while using toolkit.
ive even tried using the absolute path to a folder on my desktop and once again they load in fine in my project but they do not get loaded when opening the jar. please help ive tried everything. thanks
(btw) if i open the jar file in intellij the images load but if i open the jar file in finder or from my desktop they do not. i want to be able to send the finished project to someone
The toolkit.createImage(String) method takes in a string, and interprets it as a path, looking for a file at that path. An entry in a jar file is not itself a file. It is therefore impossible to use this method to read image files that are in a jar.
However, that's not the only createImage method. There's also toolkit.createImage(URL) and that is the one you want.
SomeClass.class.getResource("something.png")
This expression works on any class (Foo.class gets you the class instance for Foo, and all class instances have the getResource method), and will look for the named entry in the exact same place SomeClass.class (the file) lives. If SomeClass.class currently lives in a jar file, then that's where it'll look.
Thus, ensure that img.png is in the same place your class file is (ensure it is jarred along with the rest), and that will work. You can also ask for e.g. SomeClass.class.getResource("/foo/bar/img.png"); this will then look from the 'root' of where SomeClass is. So if you have a jar such that if you run jar tvf thatjar.jar and you get:
....
/com/foo/yourpackage/SomeClass.class
....
/foo/bar/img.png
then /foo/bar/img.png works.
thus: Once you know where you put that stuff in your jar file:
toolkit.createImage(YourClass.class.getResource("image1.png"))
is what you're looking for.
I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));
I am wondering how I can navigate a folder up like shown here: https://github.com/libgdx/libgdx/wiki/Texture-packer#automatic-packing
I did exactly the same ../ but libgdx is registering .. as a folder and does not go up in the tree. However the docs are kinda old and might be outdated.
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
TexturePacker.process(settings, "../images",
"../android/assets/tilesets/",
"set01");
//Input file does not exist: C:\Programming\Java\LibGDX\Projects\CloneQuest\android\assets\..\images
The reason why I want this is because I want to have a image folder with all separate images outside the assets folder. Whenever the desktop application is run in it's current state it will pack the images into the assets folder. But these separate images won't be transferred to the jar or apk when making a build.
This is working, the output just prints it like this. Are you sure there is a folder images as a sibling of assets, as you can see you are in the assets folder
C:\Programming\Java\LibGDX\Projects\CloneQuest\android\assets..
Please provide your folder structure if you struggle.
int x = new File("src/Images").list().length;
It works fine for me until I want to run it as an executable jar file.
"/Images" & "Images" won't work and I've read that I can't use new File inside a jar.
I want the jar file to run, locate the file inside and count the amount of images in the folder.
Is there a simple alternative to the above line of code?
I've tried playing with DirectoryStream and a few other things but have had no luck.
Cheers
It usually uses the directory of .jar file as root directory.
The easiest way to "make it work" is this: Having folder src in the same folder as the .jar file and in src folder put the Images folder.
So it looks like this, when you execute the .jar:
...../myproject/myproject.jar
...../myproject/src/Images
I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.