I have a java application. And It loads images from a directory
"C:\\Users\\elliotbewey\\Desktop\\IMGS\\BOB.png"
And it loads fine the image displays. Now If I was to compile it into a runnable jar and send it to my friend. it would not work. What would I need to do get a installer? And get system information? Help!
//note I do understand that its going to my friends computer so \\users\\elliotbewey would not work because its his computer thats what I need help with
You need to package your images inside your jar, then reference them as resources. Doing a google search for "java package images inside jar as resource" will give you a ton of results and tutorials.
This approach will work from an IDE on your computer or from a packaged jar on your friend's computer. Using an installer is definitely overkill for this goal.
Locate your images inside jar file (for example res/image.png).
Then simply call this to get BufferedImage:
BufferedImage image = ImageIO.read(YourClassName.getClass().getResourceAsStream("/res/image.png"));
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 want to use things like text files and png files by using BlueJ. I also want it to work on a computer which does not have the resources.
How to do this?
There are many different ways to use external resources and to ensure they are accessible from another computer.
I'm not familiar with BlueJ but if its like most other IDEs...
For code that you write and run in BlueJ you should be able to put the resources next to the compiled .java files.
Say the resource you placed there was called "img.png" and the class containing your main method was called "Main.class".
To load this file as a BufferedImage you would do the following:
BufferedImage image = ImageIO.read(Main.class.getResourcesAsStream("img.png"));
This will load the file based on its location relative to your main class file.
If you want this file to be accessible to someone running your program on a different computer the best option is to bundle it with you code into a .jar file. See THIS for instructions on how to create a jar.
That will show you how to bundle your code into a jar next you have to add the resources (the "img.png" file). To do this download Jar Splice and follow the on screen instructions, its a very simple program to use.
The output from Jar Splice will be a .jar file containing your code and your resources which can be run on any computer with java installed.
(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 made a chess game with a gui, using images for the pieces. It works perfectly when run in eclipse. However, I tried exporting the jar (from eclipse) and running it in cmd but none of the images appear (the game itself however works perfectly, i just have to guess where the pieces are).
I'm going to assume that there is an issue with the addresses of the images...
The problem is, I can't even seem to be able to open any of the files within the jar file to see what, if anything is going on.
In eclipse, all the images were in a package within the scr folder, and they are definitely within the jar.
Any advice?
If you want to load images within your jar file you will need to use the
Class.getResource(String) method.
For detailed instructions see the How to Use Icons section in the Java Tutorial.
I can run my Java Swing application from Eclipse without problems. But when I run it from a .jar-file, the images fails to load.
I load my images with:
setIconImage(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("../images/logo.png")));
How can I load the images so they work even when I'm running from a .jar-file?
The images is in the same Jar-file, in the packet com.example.images and the class where they are used is in com.example.gui.dialogs
Use the absolut path inside your jar. If you don't know it, try opening the JAR with a zip programm, e.g. 7zip. Then use the absolute path:
getClass().getResource("/com/examples/images/logo.png")
This obiously only works when your image is in your jar. If its not, but in your classpath, this should be fine too.
The images should be packed as well in a jar file. Actually, I'm not 100% sure there is no other solution, but at least I made it work this way, when experimenting the same issue.
The jar was then added to the classpath, and I'm accessing image resources this way:
getResource("images/logo.png");