I need to replace a single class file from a jar file. The jar file is quite big and every time I don't want to extract it and replace manually. I want to automate this procedure. Can someone please help me on this.
When jar file is extracted below folders are created :
render
classes
com
I need to go inside "com/cgp/f1/cmmi/" folder and replace a class file inside it.
Things I tried :
zip file.jar com/cgp/f1/cmmi/services.class Services.class
jar uf file.jar com/cgp/f1/cmmi/ services.class
jar -uf file.jar com\cgp\f1\cmmi\ services.class
jar uf file.jar com/cgp/f1/cmmi/services.class services.class
The error I am getting is :
when using jar command
com\cgp\f1\cmmi\ : no such file or directory
when using zip command :
zip warning: name not matched: com\cgp\f1\cmmi\Services.class
can some one please guide me where I am going wrong.
Maybe the jar -uf found here could help you: How to update one file in a zip archive
If graphical apps are an option, you could use winrar or 7-zip to replace the class. You don't need to extract the jar file to make this work. Just open the jar with one of those apps, go to de directory where is the class file to be replaced, drag-and-drop the new file to replace the old one and save.
Related
I am working on creating an Android app and need to import a jar containing a class that I created into it, the class doesn't contain a main method, so, as far as I understand, the jar won't be executable, which is fine. I just need it to be able to be used within the application.
How do I create the jar file?
You can try creating by using command prompt: jar cvf jar-file-name folder-of-classes
Steps to do:
1) Take all the required `.class` files place it into a folder named `xyz`in directory `D:\abc`
2) Open Command prompt and change the current working directory to D:\abc
3) run command as jar cvf test.jar xyz
check the directory D:\abc there will be a jar file named test.jar containing the folder with all the class files.
Hope it helps
jar file is just a bunch of .class file. select your android package Right click -> New -> Java Class.
copy your old class file code into newly created. Do care class name & .class file be same name.
if you mean to import java .jar dependency
Select your app. Right click goto New->Module->import .jar/.aar package
Using the Jar file in your application
In your applications Android.mk; specify the name of the JAR file under the LOCAL_JAVA_LIBRARIES :=
I have a jar file in C:\ (xx.jar). I need to replace a class file (path: com\sample\folder\xfile.class) within xx.jar with a class file (yfile.class) that is placed C:\.
Please help me with the command for replacing one class file in the jar with another class file outside the jar. I am struggling in pointing the directory structure.
I tried with,
jar uf C:\xx.jar C:\yfile.class
The above command creates a new folder C:\ within xx.jar and the yfile.class comes inside C:\. But I want the file to be inside the com.sample.folder in the xx.jar
Kindly help. Thanks in advance!
You can't, really. Jar is really meant to package a prepared folder.
I guess this is also why maven etc create a build folder with everything that is going to end up in the jar/war/ear file.
jar uf C:\xx.jar C:\yfile.class
is indeed just adding the file to the archive.
In the folder you are archiving from you can use some -C directive, but that is about it. Suppose yout myfile.class is in a dir called C:\classes and you cal jar from that folder
jar uf C:\xx.jar -C .. classes\file.class
Is going to add the folder classes to the jar with the file myclass.class in it.
Kind regards
I made a code that connects to my sqlite driver which is in the CLASSPATH and reads some database file. I want to create an executable which can be used on computers that don't have the sqlite driver.
If I do:
jar cvfe exec.jar main_class
I will get "class not found: org.sqlite.JDBC" when running with
java -jar exec.jar
What should I do to make the executable work?
Edit:
I don't know if it makes any difference, but this is the JDBC driver I use:
https://bitbucket.org/xerial/sqlite-jdbc
You need to include the library inside the JAR. Maybe you don't know this, but JAR files are just ZIP files, so you can change their contents easily. Here are some quick instructions on how to do it. Assuming your JAR file is named exec.jar, and the JAR of the library you want to include (the JAR you downloaded) is driver.jar
Change your file name from exec.jar to exec.zip.
Extract all the contents of exec.zip into folder exec/
Change your library file name from driver.jar to driver.zip
Extract all the contents of driver.zip into folder driver/
Copy the contents of driver/ into exec/, but do not copy the META-INF folder. If a pop-up asks if it's ok to merge the folders, click yes.
Compress all files in exec/ into exec.zip
Rename exec.zip to exec.jar (replace the original).
You can include any java library inside a JAR using this method.
Here is the doc:
C:\Windows\System32>jar /?
Illegal option: /
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
And so I think the command you need is:
jar cvfe exec.jar main_class main_class
How would you read a file into a program that's compiled into a jar next to it through its local directory? The type read would be a simple .txt file.
It depends on what the usage of the program is. Do you know how the jar is supposed to be executed? When you try to run it, does it spit out a "usage: somejar firstarg secondarg" type message?
Also, if its a jar that you've compiled and you know how it should be executed, then you may have forgot to set its main class or manifest.
Check this: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to read a file that exists within an external .jar file, you will need to unzip the .jar file first in your code and then retrieve the file. You can do this using Java's zip APIs. See this answer if this is the case: Easiest way to unpack a jar in java
If you want to read a file that is in the same .jar file that your code is executing, you can get the file as a resource. See this answer: Get a resource using getResource()
If the file is simply in the exact same directory as the executable .jar, create a new file like so:
File input = new File("myfile.txt");
I'm trying to create my first jar file and I'm having trouble. I'm using the DOS to do it and my path and everything are all set up correctly.
These are the files I'm trying to include:
Bot.class
Start.class
Stop.class
Thread.class
I've created a manifest file that looks like this (it also has a carriage return at the end)
Main-Class: Bot
Here is what I'm running through the DOS
jar cf Bot.jar Manifest.txt Bot.class Start.class Stop.class Visit.class
It creates the jar successfully and but when I try and execute it, either by clicking on it or using the command prompt I get
Could not find the main class: Bot. Program will exit.
I have tried using both Bot and Bot.class in the manifest file but still get the same error. I've only tried changing the extension on the manifest file to .mf instead of .txt
Thanks in advance for your help!
jar cmf Manifest.txt Bot.jar Bot.class Start.class Stop.class Visit.class
The m switch is needed to tell jar which file is the manifest. Note that the order of m and f must match the order of Manifest.txt and bot.jar
Remember that the directory structure in the jar must match your package structure.
e.g. for package com.me.Bot the added file should be com/me/Bot.class not just Bot.class
learning how to manually build a jar is a fun thing to do once. for the long term, however, i'd recommend learning a build tool (ant, maven, whatever).