32

I have a jar file from an external source. All of the classes in the jar are in the com.xyz package.

I would like to move all of classes to the com.xyzold package.

Is this is simple as unzipping the jar, renaming the xzy folder to xyzold, and rezipping it, or do I need to modify each class file as well?


Here's my solution, using Jar Jar Links

java -jar jarjar-1.4.jar process rules.txt google-collections-1.0.jar google-collections-old-1.0.jar

And here's the contents of my rules.txt file:

rule com.google.** com.googleold.@1
mernst
  • 7,437
  • 30
  • 45
Ben Noland
  • 34,230
  • 18
  • 50
  • 51
  • 1
    why do you need to move classes to different package? – Parvin Gasimzade Dec 06 '12 at 15:24
  • 4
    They have a new project, which uses the same package and class names. It was supposed to be fully backward compatible but isn't. We have an very large codebase that depends on the old version. – Ben Noland Dec 06 '12 at 15:50
  • That command line invocation doesn't work with the version of jarjar I found at https://github.com/hannesa2/jarjar/releases/tag/1.0.3. Parameters have to be passed with "--" e.g. `java -jar "jarjar-command-1.0.3.jar" --mode process --rules "rules.txt" in.jar --output out.jar` – abulka May 23 '23 at 02:18

6 Answers6

23

You can use Jar Jar Links to achieve that. Also you don't need the source code of the classes you'd like to modify, as the program does it on the bytecode level (ie. it modifies the .class files directly.)

Maik Schreiber
  • 246
  • 1
  • 3
2

You can use the Maven shade plugin to do this in a Maven build.

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • This worked for me. Though it did introduce extra classes from my project into the jar that I didn't need, so I had to delete them manually (by unzipping/zipping using jar) – abulka May 23 '23 at 22:46
1

You should take a look at Jar Jar Links. (I'm not taking any responisibility for the name :).)

Its primary use case is to embed external jars in your own jar, and to do that, it can re-name existing packages. It changes the necessary byte code. You might be able to tweak it to do what you want.

jqno
  • 15,133
  • 7
  • 57
  • 84
1

You can probably do this with the ProGuard obfuscator. It can certainly move code around in packages and operates at the byte code level. Its primary aim is obfuscation though so one of the other options should certainly be looked at first.

pillingworth
  • 3,238
  • 2
  • 24
  • 49
-4

No you need to modify all the classes in jar file. You need to have a source code in-order to change package.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
-4

You would need to modify all the Java source files too. Not just the package names but the imports too, and any possible inline declarations. Not really worth it.

Zutty
  • 5,357
  • 26
  • 31