Creating and modifying files inside jar [duplicate] - java

This question already has answers here:
How can an app use files inside the JAR for read and write?
(5 answers)
Closed 8 years ago.
Is it possible to create or modify files inside jar and if it is how to accomplish that?

One possible solution is to unzip the jar, modify it and zip it back up.
NOTE: You can extract it with a utility like 7-zip or WinRar.

You can use jar command if you would like to do it through command prompt (or) you may use winzip (or) winrar tools.
Example:
jar uf foo.jar -C classes . -C bin xyz.class

Ok here are two ways to do this:
Use your favorite zip application, unzip it, change the files and zip it again.
Use a zip library like Zip4J and do the same in your java application.

In a word, yes its possible and you have several options:
Modify the existing jar file. Do this by extracting the jar file (use whatever extractor you need to), modify the file you want (decompile if you don't have the source) and then repackage the jar.
If you don't want to go thru the trouble of changing the jar, you want to copy the existing class file (again decompile if you don't have the source), make the changes you want, compile it and make sure your class file is ahead of the jar in your CLASSPATH. That way when your VM does its classloading, it'll get your modified class first and ignore the one in the jar.

Related

How to Compile .java files into .jar file? [duplicate]

This question already has answers here:
How do I make a JAR from a .java file?
(8 answers)
Closed 6 years ago.
I just have some (134) .java source files with me and I'm pretty sure that contains all the necessary code!
I want some quick tutorial to compile this program into a .jar file.
(I'm on a windows platform btw)
I have tried javac and jar commands. I got my .jar file but it's not opening!!!
Thanks in advance!
Best practice is to make use of Ant/Maven/Gradle kind of build program tools that can take care of creating Jar files.
Other way is to just make use of Eclipse's feature for exporting the project as a light weight Jar file or Runnable Jar file(which includes dependencies).
Place all the files you want to include in the JAR file inside a
single folder.
Open the command prompt in Admin Mode
Navigate to the folder where you stored your files.
Set the path to the directory of the JDK bin. You will need to run
the jar.exe utility to create a JAR file, and that file is located
in the bin directory.
Create the JAR file.
The format of the command line for creating the
JAR file looks like this: jar cf 'jar-file'.jar input-file(s)
You can use WINRAR.
right click on file (put inside all your .java files) and compile by using winrar;
choose format .zip (important)
and save filename.jar (important)

How to make jar file without using any IDE [duplicate]

This question already has answers here:
Java creating .jar file
(5 answers)
Closed 8 years ago.
I decompile jar file using jd-gui app. I made some changes on it.
After that i try to compile main.java file which one include all packages & other java files.
Then it Shows classnotfound exception.
I need to it without using any IDE.
Please Help me.
Thanks in advance.
A JAR (http://en.wikipedia.org/wiki/JAR_%28file_format%29) file is a ZIP-compatible archive of your compiled classes and other binary resource.
Feel free to unpack/pack it or treat it the way you treat ZIP-archive, say using your favorite archiving tool, like 7zip.
You can even create a ZIP archive with the contents you wish your JAR to contain and simply change the file extension to .jar.
NOTE: in case you use compression for your ZIP-archive, because in that case your manifest often must be the first entry in the archive.

Extract a JAR file, possibly with WINRAR on CL

I need to be able to extract jar files on the command line.
Piece of cake, you might say. Yes, except I need the extractor to rename same-named files.
To be specific, the Jar file has a file named:
classAX.class
and another named:
classax.class
in the same directory. I need it to extract them both, and preferably rename one:
classax (2).class
or something similar.
It must be able to do this without any user intervention.
Winrar has this capability, but when attempting to extract with unrar.exe, it says that the input file is not a valid RAR archive. (Though winrar has jar capabilities.)
Any way to force winrar to accept jars via command line, or perhaps another program?
Thanks,
~Kurt Nauck
You can develop your own programme in java, there is a Zip package that can deal with compressed files, an also you can deal with a rest of your needs
7Zip command line util accomplishes this perfectly. Thanks for the answers.

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