First, let me clarify that I'm a Java programmer by hobby, English isn't my native language, and this is my first question in StackOverflow, so if I'm doing something wrong, I will be grateful in be advised.
When I need to use some image in my applications, I put the image file in bin folder of my project and use the following code to acces that image:
Image img1 = new ImageIcon(this.getClass().getResource("/confirm.png")).getImage();
Recently, I decided let user choose the image with a JFileChooser. The way that I made is get the image path and copy that image into my bin folder, so this way I can use the above line of code. Here's is how I'm doing that:
File outputFile = new File(System.getProperty("user.dir")+ "/bin/" + "default.png");
ImageIO.write(defaultImage, "png", outputFile);
That way I can get a copy of the file in my project folder (C:\Users\renan\Desktop\Image_Editor\bin\default.png).
Everything works fine when I run the application in Eclise, but the things start to go wrong when I create a Runnable JAR file (.jar). When I runed my .jar file over command prompt (java -jar "Image Editor.jar") I've got the following error:
java.io.FileNotFoundException: C:\Users\renan\Desktop\bin\default.png (The system can not find the path specified).
After think about this error, I realised that after create a .jar file I can't stay copying the images into my bin folder because now has just a .jar file, not separated folders like before.
My question is: What should I do to deal with images that will be used after the program had compiled?
Should I create a folder in my computer that will storage this application images and load the images from that folder? (like games have Screenshots folder to save their images catched by the user).
If anyone can advise me the right way to do this, I will very thankful.
Create a folder outside of the JAR. A JAR file essentially a *.zip file on Windows, therefore you can't change any of the folders in the JAR file after it's been compiled because it's in a compressed format.
Related
I'm currently learning Java and I'm doing a project of html "creation".
The point its than I want to store an image in the jar file and be able to access it in run-time (I'm going to copy the image to a certain folder), but i don't know where to put the image, how to access it from my code, or how to compile it properly.
I'm currently using ant for the compilation process.
I should give my professor the whole source code and resources, then, he should be able to run ant proyectX and it should create the .jar file as an stand-alone app.
I don't want to use a external URL because he is going to test it and maybe, after seeing the code, he would disconnect himself from the internet.
I can't use any external library, just pure java code.
Thank you, and sorry for the bad english.
You should create a folder in your jar (for example "img") where you will keep all images.
After that you can get the image this way:
InputStream input = getServletContext().getResourceAsStream("/img/image_1.jpg");
Or this way:
File file = getRequest().getServletContext().getRealPath("/img/image_1.jpg");
Loading a file image in a war project
File path to resource in our war/WEB-INF folder?
This is my first question here and probably it is too cliche to answer, but I hope for the best.
I made an app, which reads .gif files from /bin folder using ImageIO.read.
In code, there is:
File imageFile = new File("bin/logo.gif");
and everything works fine, but after compiling th eapp and running it from .jar file, there is an error "Can't read input file!". I had this problem inside Eclipse, so I wrote "/bin" in the file reading path.
What should I do?
Keeping the .gif files in the bin is not a good option. It is a better approach to make a separate folder (For example, /images) in your /src directory and access your resource files from there.
I got the answer: File gets the image from the catalogue in which is the .jar file. setSpriteName gets images from catalogues inside .jar file.
In my current code I write the specific location of the images I want to use in my project, now, these locations are only correct until I move the image to a different directory or open the application in a different computer.
Where do I place the images (where to place them in the exported folder) I want to use in my project?
The exported project is a zip file, once extracted, I have 2 folders within the extracted folder, one named nbproject other is src, one text file named build another .mf file called manifest inside nbproject I have two text files and two .properties files inside src I have my four classes.
Where do I put the images I want to use in the project? And once I place them, what directory do I write in my project?
Here's an example of how I use an image:
// Main menu background image
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
And then I draw the image all over the screen
My exported folder contents:
http://i.imgur.com/qd4PeJo.png
http://i.imgur.com/W5YQ7OC.png
EDIT: Thought it worked but it keeps giving an exception on other people's computers, perhaps this isn't the reason beause I moved the images around in my PC and it worked but still.
Since you are writing the directory in your code.
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
When you run the project in another computer or move the image to another location, the program can't find the path to the picture, so there is an error. Instead of using the full path. Copy the images to your project folder and use relative paths.For instance : "images/Vanguard.jpg".
The path where your program looks for resources can depend on how you run the program. If you ran it via console/terminal the initial path starts at the path of the *.class file you ran. If you run it from an IDE this path may change, in Eclipse the path starts at the Project dir and not the src folder. You can find out the exact path by calling System.getProperty("user.dir") that will return the current working path as a String.
Don't use a strict path, use a relative path like so:
Main.java
String path = "Project Images/bg option for Vanguard.jpg";
bgg[0] = new ImageIcon(getClass().getResource(path)).getImage();
In order for this to be loaded, you must place a folder called Project Images in the directory in which your Java files are. Then place the image bg option for Vanguard.jpg in this folder. Be sure to compile the program in you IDE so it can make a copy for the compiled version.
Swing application and it's Images are running fine in eclipse. but when i run from jar file, images not displaying. I tryed to load images in different way, but some time jar file not executing.
My image folder is inside of source folder.
I load my images with:
1.Running fine in eclipse, but not in jar
ImageIcon right = new ImageIcon("./chack img/Green Tick 1.PNG");
2.Running fine in eclipse, but jar file not executing.
ImageIcon wrong = new javax.swing.ImageIcon(getClass().getResource("/chack img/Red Cross1.PNG"));
I tryed some other code also, still my issue not solved.
Put your images in the src folder, along with your classes. Optional, but make a package that stores your images. Compiling a jar file will not keep any directories that are not a source directory.
Also, remove all spaces from your folders and file names. This prevents confusion, and is likely part of your problem. No spacing is a good practice to follow in programming, and in most situations of naming folders and files.
Also, kiheru is correct, you should not have capital letters in the file extensions, for the same reason you should not have spaces; It can cause confusion and it is a bad practice in most cases.
EDIT: Simmilar Question
How would I load a .dll in a way that when the project is transferred to another computer, the application would still work perfectly without changing the .dll path?
I read some threads on this website who tried to answer that question it seems, but all of them went over my head. Please explain in very basic terms (ELI5).
Thank you.
If you know where the dll file will be located in relation to the launching point of the application (i.e. the jar file), you can get the current working directory, then put together a relative path that way.
Get the current working directory using this line:
String directory = new File(".").getCanonicalPath();
If the dll is in the same folder as the jar then you can locate it like this:
File dllPath = new File (directory+File.separator+"example.dll");