How rebuild a jar file? - java

I have a jar file with following directory:
css/
images/
js/
META-INF/
atlassian-plugin.xml
jira_rtl.properties
LICENSE
I want to change the file.css in css directory and then rebuild the jar file.
How Can I do this in Windows 8.1 OS?

Use the following command to update the file via Java's jar utility,
<JAVA_HOME>/bin/jar -uvf <jar-file-name> <modified file path>
In your case lets say the jar file name is simple.jar,
<JAVA_HOME>/bin/jar -uvf simple.jar css/*

Use WinRAR (or similar) to unzip the .jar file and replace the files you need and then re-zip it (using a .jar file extension). You may need to rename [filename].jar to [filename].zip and vice-versa.
Some archivers like 7-zip even enable you to directly edit from the program and after you save your edits it will automatically update your archive (in this case a zip with a .jar extension). So this is even easier than unpacking all files and packing them again.
There are rare cases where the order of files in the jar matters (OSGi bundles for example), so you have to be careful. But I would say in 99,9% the order does not matter at all.
Java ARchive (.jar) format is (just) a zipped file with same format as .zip, that is why this can be done. The same .zip format is used in other archive formats (for example .swc for ActionScript/Flash)

Related

Theoretical sequence: how is a .jar file created?

How are .jars created?
I know that an IDE like Eclipse can create Bytecode (.class) from developed Sourcecode (.java). And it can under "Export" create an .jar.
And know i want to know: Is an .jar created:
direct from Sourcecode?
from Bytecode which was copied?
with a totally other technique?
Source code files (.java files) are compiled to bytecode by Java Compiler. Bytecode is then stored in .class files.
These files are then packed together using jar tool to create JAR (Java Archive) file. JAR file is a zip archive usually containing:
.class files,
jar manifest
application resources
You can read more about Jar files on oficial documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
A .jar is just an archive of .class files along with associated resources (if any) and metadata (if any). In fact, "jar" means Java ARchive, and the file format is the same as .zip files. From the Oracle JAR file overview :
JAR consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files, as defined in the JAR File Specification.
So source code is compiled to .class files (bytecode) which is then wrapped up in a .jar archive via the jar tool (or anything else that can correctly create .jar files per the spec).

How can I change all .class files in a jar(or folder) to .java (or .txt) at once?

I have always used this to decompile any .class file to readable format. However, now I need to change around 30-40 .class files to readable format to push to my github. How may I change all the .class files to readable (.java or .txt) at once?
JAR (or ZIP) all the classes you want decompiled together.
Launch JD-GUI and open your jar (zip) archive.
Use "File" - "Save JAR Sources" menu option. It will decompile everything from your archive and save their sources into another zip file.
You would then need to unzip the ZIP file, and add it to a git repo before push.

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

how to update a params in a jar file?

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)

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