Accessing a .jpg file that is in a Jar - java

I have a question about accessing a .jpg file that is contained within a ant generated java jar file. My java project contains Java code and some .jpg files that I use. I want to be able to reference that .jpg file and be able to copy from jar to a directory.
Is there any way to that or do I always have to keep the jar outside of the jar file?

You can import the jar as a library into your project and access the resources directory or wherever you placed the JPG if you want to reference it directly from your project.

Jar files are just .zip's with a manifest. If you know where in the jar file the jpg is place, you just need to unzip the jar and copy the jpg to the desired location.

Related

make jar file in different folder

when making jar file in java, how to make jar file in a different folder where other files are not located?
I have two folders f1&f2
in f1/ft1/ft2/ft3 I have the java code in f2/ft1 I have few csv fine files to be used in the java code, and I have to create jar in f2/ft2. how to do it?

Create a .class file from Java file which is present inside a jar without unpacking the jar

I have opened a jar file in a Winzip, there is a Java file that need some modification, modified it inside jar. Now the Java file's .class is needed. How can I generate class file from a Java File which is inside the Jar. Do let me know any command line available. I cannot unpack the jar as there are some class files inside that is not open source. Overall I want to add a line of code to java file and replace class file with update version of java file.

Is there a possible way to copy a file to a jar file?

I would like to ask a question if there is a way to copy a file(i.e. an image) to a .jar file, because i want to deploy a program and i created folders along with the source codes(they are in the same location as the source codes) and the folders are inside the jar file (i used Netbeans to create the jar file)..... Now what i want is i would like to copy files choosen by a JFileChooser to the folders inside the jar file????
Any idea is heartily accepted..!!! Thanks in advance????
P.S.:
I already tried searching the net for answers but all they know is how to copy the file inside the jar file to another directory......
Suppose that you want to add the file images/image1.gif to the JAR file.
jar uf <fileName>.jar images/image1.gif
To change the directory use -c
jar uf <fileName>.jar -C
in this command
jar uf jar-file input-file(s)
In this command:
The u option indicates that you want to update an existing JAR file.
The f option indicates that the JAR file to update is specified on the command line.
jar-file is the existing JAR file that's to be updated.
input-file(s) is a space-delimited list of one or more files that you want to add to the Jar file.
Related Docs
A JAR file is a ZIP compressed file.
See this S.O. answer for a solutiion to add files to an exisiting ZIP archive: Appending files to a zip file with Java

modify properties file in META-INF

I'm using maven and storing a properties file in src/main/resources which I can read fine like:
properties.loadFromXML(this.getClass().getClassLoader().
getResourceAsStream("META-INF/properties.xml");
Is it possible to then write to this file when it is packaged up as a jar? I've tried the following:
properties.storeToXML(new FileOutputStream(new File(this.getClass().getClassLoader().
getResource("META-INF/properties.xml").toURI())), "");
This works in Eclipse because it is saving the file to target/classes/META-INF, but not when packaged as a jar, so is it possible to achieve the same thing?
Is it possible to then write to this file when it is packaged up as a jar?
Short answer: no, it is not possible, a jar file is a file, not a directory. And actually, you generally don't write properties files back to a jar file.
You should put that properties file on the classpath on the local file system, outside a JAR.
It's always possible to modify a packaged file by unpacking, rewriting and re-packing. Sometimes this is the easiest approach.
A Jar file is essentially a renamed .ZIP file. Java has classes for accessing files within a .Zip file, and you could (if sufficiently motivated) write yourself a program to do this.
Alternatively, I'm pretty sure there are ant tasks that can do this too (think creative use of the jar task), and there are POM plugins available to run ant tasks from Maven.

Find text files in Jar

I have an app which has to read from a text file (using FileInputStream). The text file is in the directory structure relative to the class file (eg. "../textdir/text.txt"). When I run it normally (ie specifying the /bin folder containing the .class file in the cp) everything works fine. However, I somehow need to package everything into one jar and when I run the jar nothing works. The error is something like "FileNotFOund: MyJar.jar!/textdir/text.txt". I ran jar -tvf on the jarfile and the text file was indeed inside. I have read but not write access to the source code.
More than trying to solve my problem (I think there are plenty of workarounds), can someone explain to me how the whole thing work? How does the jar search for files? What if I want to read from current working directory of the command prompt instead of the directory of the .class in the jar file? Also, I recently had a similar problem with loading resources when I converted a non-jar project to a jar, how does that work?
Instead of opening the file as a FileInputStream, use getResourceAsStream which will work in both of your contexts ie. within the jar file or unpacked.

Categories

Resources