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?
Related
I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader
I'm currently making an executable JAR file which I want to be able to send to other computers. I have a loading/splash screen at the start at the program which uses a picture from my computer.
I'm currently using gradle to create the JAR file maybe that could be used?
Otherwise, I was thinking that I would send a zip-file containing the jar file and the picture and then in someway write the path in such a way so that it always finds the picture?
This is how I get the picture for my program.
new ImageIcon("/Users/UserA/Desktop/SheetsQuickstart/AD3.gif")
Put your image file under a package (com.myproject.images) in your project and refer it like below:-
new ImageIcon(this.getClass().getResource("images/AD3.gif"));
Put the file containing your icon in your classpath (i.e. with your classes in your jar file) and use getResourceAsStream to access your image as a bytestream when you need it.
Note that this is notoriously hard to get right the first time. You might want to look at getResourceAsStream returns null if you run into trouble.
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.
I have a jar file that's running, but it needs an exe and a couple dlls with it to work.
To make it convenient for the user, I want to package the folder with the exe and the 2 dlls in the jar, and have it extract it when the jar is ran.
I've read answers like this one, and this one, but I still don't understand how to apply that code to what I need. I understand that a jar file is essentially a zip, but I don't know how I can get the path to the zip, and extract the folder I need from it.
I tried using the code posted here to just extract the exe for a start, but it looks like it's trying to extract the exe from the class (if that makes any sense?)
Does anyone have a code snipped they could share to show how to extract a folder with a certain name from the currently executing jar?
If you guys could help me out I would really appreciate it!
If you want to access the resources from a JAR that is on the classpath at runtime, you simply access the resources from the classpath. So no need to read it via the JAR API. Therefore your first link points to a valid solution.
It may be tricky to use the appropriate classloader. If your resource is in the same folder as a Java class file, this link may help you: How to access resources in JAR file?
URL url = getClass().getResource("path/to/img.png");
or
URL url = MyClass.class.getResource("path/to/img.png");
I think you could access the two files separately and store them to a file. There are also methods to open a stream to copy. Here is a similar question: getResourceAsStream returns null
I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that.
I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder.
I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equals(provider.getScheme()))
return provider.newFileSystem((new File(Start.class
.getProtectionDomain().getCodeSource().getLocation().toURI()))
.toPath(), new HashMap<String, Object>());
}
If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.
To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.
fileSystem.getPath("/images/image.gif")
If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.
new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toPath().toString();
This will allow you to run your program whether it's compressed into a JAR file or not.
I recommend you get used to using NIO.2, since it is a very powerful API.
If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !