how to update a params in a jar file? - java

I need to update a config.params in within a jar file that was compiled by ant. I know I don't need to re-compile the source java code for doing this. Can someone help me with how to update the params file in jar file?
I need to change an integer value n the config.params file:
fileSize = 4 should be changed to fileSize = 20

Try extracting jar in a zip program like 7-Zip (or rename to .zip and open with Windows), change the file data, then re-zip (and rename back to jar if you need to).
Technically the "compiling" was done by javac (not Ant) to build .class files, which you don't need to touch since the developers were smart enough to put the setting you need into a properties file.

I did the following:
jar xf jar-file [archived-file(s)]
modified the file I wanted
jar cf jar-file input-file(s)
learned from http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
an even easier way is this: jar uf jar-file input-file(s)
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html (no need to touch the jar file at all)

Related

How to recompile jar file from command line

I have a jar file that consists of class files and java source files together (actually android unity plugin).
I want change the behaviour of one of the function by modifying the java source code and repackage it to jar file. Is it feasible to do with command line?
Use jar xf <JAR-file> to extract the entire JAR file to whatever directory you're currently on.
Add your new code to the files, removing the old code (make sure you have copies or back everything up, just in case).
Use jar cvf <JAR-file-name> * to create a JAR using all contents in the directory of your files.

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

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?

So I need to remove a file from a jar / war file.
I was hoping there was something like "jar -d myjar.jar file_I_donot_need.txt"
But right now the only way I can see of doing this from my Linux command line (without using WinRAR/Winzip or linux equivalent) is to
Do "jar -xvf" and extract the
complete Jar file
Remove the file(s) I don't need
Rejar the jar file using "jar -cvf"
Please tell me there is a shorter way?
zip -d file.jar unwanted_file.txt
jar is just a zip file after all. Definitely much faster than uncompressing/recompressing.
In case you want to delete file in order to unsign signed jar, you can probably just make the .RSA file zero-sized. This can be accomplished with just jar u. See https://stackoverflow.com/a/24678645/653539 . (Worked for me, though I admit it's hack.)
In Java you can copy all the entries of a jar except the one you want to delete. i.e. you have to make a copy but don't need to create the individual files.
You can do this by
creating a new jar.
iterating though the Jar you have
copy the entry from one jar to the other, skipping any files you want.
close and replace the orginal jar if you want.
If you wish to do this programatically, can use the Zip File System to treat zip/jar files as a file system. This will allow you to edit, delete, and add files to the jar file.
See Appending files to a zip file with Java
The best answer for me was in a comment by lapo on another answer on this post.
User lapo wrote:
I more often have p7zip installed instead of zip and in this case it's important to specify file format: 7z d -tzip file.jar dir/unwanted_file.txt
User lapo's comment is in response to an answer suggesting using zip -d directly since the .jar files are like .zip archives but not exactly the same format. Like others, I wasn't able to use zip -d file.jar unwanted_file.txt. That would lead to
zip error: Zip file structure invalid
But, I was able to install p7zip via brew install p7zip, and then I was able to delete using 7z d -tzip file.jar unwanted_file.txt.

Modifying a jar file

I have a jar file which is used in html file as applet. I want to modify the content of the jar file and to rebuild the jar file so that the html will work fine with the new jar file. How can i do this??
I already tried unzipping using 7zip nad modified the source and created the new jar. But when i use it in html it shows some java.lang.Classnotfound error
You can unjar or rejar the classes and source files as you wish.
unjar
jar -xvf abc.jar
jar
jar cf abc.jar input-files
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Make the changes in the code (.java files), recompile to get the .class files. Then simply replace the old .class files in the jar with the new ones. I usually use WinZip, but you can use whatever app that can handle .Zip files. It should just work.
I've faced cases where the launcher of the app uses some sort of verification and checks for this kind of changes. I had to use a new launch script. This doesn't seem to be your case though.
This is surely possible from the command line. Use the u option for jar
From the Java Tutorials:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
See Updating a JAR File
A brief test shows this quickly updates changes apart from trying to delete the file.
I haven't seen this answer on other threads about modifying jar files, and many, marked as duplicates, suggest there is no alternative but to remake the jar completely. Please correct if wrong.
JARs are just ZIP files, use whatever utility you like and edit away!
Disclaimer: When reverse engineering any code be sure that you are staying within the limits of the law and adhering to the license of that code.
Follow the instructions above to unpack the JAR.
Find the original source of the JAR (perhaps its on SourceForge) and download the source, modify the source, and rebuild your own JAR.
You can also decompile the class files in the JAR. This is a rather advanced process and has a lot of "gotchas".

Categories

Resources