How to inject MANIFEST.MF into jar - java

I was trying to, for debugging purposes, inject a (hacked) META-INF/MANIFEST.MF file into a jar. I created a META-INF dir at the same level where the jar is and created my MANIFEST.MF file in it. Then, per this tutorial:
jar uf myjar-with-dependencies.jar META-INF/MANIFEST.MF
which executed without error but just wiped out the existing manifest without replacing it. I know it because I ran:
jar tf myjar-with-dependencies.jar | grep MANIFEST
which, before the update, returned the found file in the jar but does not now after the update.

"jar ufm" is what you're after. The m argument specifies that you're providing a manifest file.
jar ufm <yourJar> <yourManifest>
Otherwise it just treats it as a regular file which will end up getting stomped on when it generates the default manifest file.

A Jar is just a zip file. You can open it up using any tool you would normally use to open a zip file and then just place your file inside of it where you want.

Related

Corrupted jar file

I "converted" the jar file into a zip folder
and then to a normal folder. I opened the MANIFEST.MF file with the text editor. Then I closed it and made the folder to a jar file. When I double clicked the jar file, it says "corrupted jar file". I don't know why; is it because I opened the MANIFEST file with the text editor or why?
In the future, you can use the jar command to remove files and add them back into a jar.
List all files in the jar:
jar tvf application.jar
Extract a file from the jar:
jar xvf application.jar path/of/file/to/extract
Add a file back into the jar:
jar uvf application.jar path/of/file/to/extract
MANIFEST files get a little special handling. If you want to add it back in:
jar uvfm application.jar mymanifest
Note: In all the commands above the v flag is used to provide verbose feedback from the command and can be omitted if you want.
If I understood right, your new jar contains a redundant folder because you zipped the folder, but what you had to do is to zip only content of that folder.

How to get java main class from jar file

I have an executable jar and i want to know the name of java main class from this jar
My question are if there is a java cmd to get this from jar metadata ?
You can use unix command :
unzip -p XXX.jar META-INF/MANIFEST.MF
or you can use jar commad
jar tf XXX.jar and see the content of MANIFEST.MF.
Technically a jar file can contain more than one main class.
When java executes a jar file, it looks in the META-INF/MANIFEST.MF file inside the jar to find the entrypoint.
There is no direct command to get this information, but you can unpack the jar (it's just a zip file) and look into the manifest yourself.
More about the manifest: https://docs.oracle.com/javase/tutorial/deployment/jar/defman.html
More about application entrypoints: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Make jar file from folder

i have a folder on my desktop named theme.I am new to Linux totally.
So what do I do to create a jar file from this folder?
I found this example but It doesn't work for me.
jar -cvf theme.jar
You are failing to specify the input files for the jar.
Navigate to the theme/ directory using the cd command in Terminal. If you want to include everything in this directory in your jar file, use the following command.
jar cvf theme *
Note that the * character (wildcard) indicates you want to include all files and subdirectories.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Jar files are really just zip files, renamed to .jar. Just use any folder zipping tool (such as the zip command).
zip -r theme.jar /path/to/desktop/theme

Include a class file into a JAR file

Can anyone please tell me how to add a class file into particular package inside a JAR file using command prompt.
Example: Test.jar has a packaging structure com.test
Now I want to add a class file called Test.class into com.test package of Test.jar file.
Your help will be highly appreciated.
You can update a JAR file by passing the 'u' argument and supplying the JAR and file you want to add:
jar uf Test.jar com/test/Test.class
More info about this option here.
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.
Any files already in the archive having the same pathname as a file being added will be overwritten.
Ref : http://docs.oracle.com/javase/tutorial/deployment/jar/update.html

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

Categories

Resources