JInjector is a Java tool that lets you change Java classes on-the-go using easy-to-edit text files. It works with classes in the JDK or JAR files without needing to recompile code. By using patch files in a Java-like format, jinjector allows you to add or change code directly in existing Java methods.
- Real-Time Class Modification: Update Java classes in runtime with simple text patch files.
- No Need for Compilation: Patch files do not require compilation, allowing for rapid development cycles.
- Javassist-Compatible Syntax: Leverage the powerful Javassist syntax to modify classes and methods.
To get started, add jinjector as a Java agent when launching your JVM:
-javaagent:jinjector=patch=patch_file1.hswp,patch=patch_file2.hswp
Patch files are in text format and contain details of the changes to be applied to the running program. Each file lists, for each tranformed class, the names of the fields, constructors, and methods to be modified and the location in those methods where the code should be inserted. The location can be insertBefore
(to insert code at the beginning of the method), insertAfter
(to insert code at the end of the method), or setBody
to replace the entire method. It is also possible to create new fields, constructors or methods.
@Transform(onStart)
class java.util.ServiceLoader {
$field(firstName).rename(_firstName);
$field.new() {
private String field1;
}
$method(newLookupIterator())
.insertBefore {
System.out.println("Start newLookupIterator().");
}
.insertAfter {
System.out.println("End newLookupIterator().");
}
$method(newLookupIterator())
.setBody {
System.out.println("Hello from jinjector.");
}
$method.new() {
public void someMethod() {
System.out.println("new method.");
}
}
}
Available at https://github.com/skybber/ServiceLoaderExample
- Download the jinjector agent from the latest releases.
- Include the agent in your JVM startup command using the
-javaagent
option, as demonstrated in the Usage section.
If you prefer to build jinjector from the source, follow these steps:
- Clone the repository:
git clone https://github.com/skybber/jinjector
- Navigate into the project directory:
cd jinjector
- Build the project (requires Maven):
mvn package
The build process will generate a jinjector.jar
in the target
directory.
Contributions to jinjector are welcome! Whether it's bug reports, feature suggestions, or direct code contributions, all forms of feedback and help are appreciated.
- Fork the repository on GitHub.
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request.