Replacing a class file inside a folder in a jar file - java

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

Related

Replace a single file from jar file

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.

Creating a .jar assistance

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 :=

Jar executable with sqlite driver

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

Load a file outside of a Jar

I have a folder called lib that contains all my Jar files and in one of the Jar files class, I have a main method which is called by a batch file. In the same folder location as my lib, I have another folder structure path/to/a/resource/myresource.txt
How can I load this file from a class inside the Jar file? I tried the following and both resulted in null:
getClass().getResource("path/to/a/resource/myresource.txt")
getClass().getClassLoader().getResource("path/to/a/resource/myresource.txt")
Any ideas? Even with an absolute path, it failed! Any suggestions?
You can use:
getClass().getResourceAsStream("path/to/a/resource/myresource.txt")
However, for this to work, you need to add the path '.' to the Class-Path entry of the JAR's MANIFEST.MF file.
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Two things you tried are used to read files from class-path since this folder is not on your classpath you can read it directly with any of the java File IO classes.
File file = new File("C:/folder/myFile.txt");
or if you know the relative path:
File file = new File("../../path/myFile.txt");
Your path seems not to be precise enough. Further, this question has been worked before.
Have a look here:
How to Load File Outside of, but Relative to, the JAR?
How to get the path of a running JAR file?
You can either load the file from file system
new FileReader(relativeOrAbsoluteFilesystemLocation)
or you can add the directory in question to your classpath:
java -cp "lib/*;lib" ...
and then use your original method.
(Unix uses : rather than ; as classpath separator)

Creating a .jar file - Get "Could not find main class" even with manifest.txt WITH a carriage return

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).

Categories

Resources