Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repackage .class files from the default package using jarjar?

Is it possible to move the .class files in a jar from the default package to another named package so that I can import and use them?

After browsing other questions here, I think jarjar might be exactly what im looking for, but I don't have any idea how I can use it.

So my question is how to use jarjar to repackage .class files from the default package to another named package?

EDIT: I want to clarify something - I do not have the sources of the jar I want to repackage, so I don't know how I could recompile the code. Obfuscation and reflection is out of the question.

EDIT2: It appears its extremely simple to do using jarjar :) java -jar jarjar.jar process rules.rules in.jar out.jar

rules.rules look something like this: rule a net.ylivay.src.a

Problem solved!

like image 915
YLivay Avatar asked Nov 05 '22 10:11

YLivay


1 Answers

To move classes into a different package, you not only need to change their location in the jar (or filesystem) directory structure, you also need to change the package declaration in the source files and then recompile the classes.

Instead of trying to repackage a jar, you should probably look at the powerful refactoring tools available in IDEs like Eclipse, which make this kind of thing simple. But since you say (in update to your question) that you don't have the source files, that is not possible.

The alternative would be to create proxy classes which invoke the ones you need in the default package using reflection. But not only is that unpleasant, but you say that reflection is out of the question.

So you cannot do what you want to.

like image 56
sudocode Avatar answered Nov 15 '22 11:11

sudocode