Skip to content

Obfuscate Java source ​

Requires Codefend 3.3.0 or later for the recommended m_ preset.

This example transforms one Java source file, compiles each version, and compares an order-total calculation. Codefend changes selected source text; it does not process .class files or JARs.

1. Add the Java source ​

Install Node.js and JDK 11 or later. Install Codefend once on your development computer:

sh
npm install --global codefend

Create source/Main.java:

java
public class Main {
    private static int m_total(int m_unitPrice, int m_count, int m_discount) {
        return m_unitPrice * m_count - m_discount;
    }

    public static void main(String[] args) {
        System.out.println(m_total(20, 4, 5));
    }
}

Expected obfuscated source ​

With the configuration in the next step, Codefend produces:

java
public class Main {
    private static int Ox0(int Ox1, int Ox2, int Ox3) {
        return Ox1 * Ox2 - Ox3;
    }

    public static void main(String[] args) {
        System.out.println(Ox0(20, 4, 5));
    }
}

The program's expected output remains 75. Generated identifier numbers can change when the source or Codefend version changes.

Preserve the public class name Main, its matching Main.java filename, and public static void main. Only the helper and parameters we own use m_. System.out.println belongs to the Java platform and stays unchanged. This standalone example needs no Maven, Gradle, or npm project files.

2. Configure selective matching ​

Create .codefendrc.json in the project root:

json
{
  "generation": {
    "inputDir": "source",
    "outputDir": "release"
  },
  "parser": {
    "name": "startWithMAndUnderscore"
  }
}

The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.

It does not match Main.java. Keep only source text under source. Reserve sibling release for generated source because Codefend replaces its output directory. Compiled classes belong outside both trees.

3. Run and compare ​

Run from the project root:

sh
java source/Main.java
codefend --obfuscate
java release/Main.java

Both programs should print:

text
75

The JDK's source-file launcher compiles and runs this single file directly, so no explicit javac command or class-output directory is needed. Inspect release/Main.java for the renamed helper and parameters. Their exact generated names can change with the input.

Preserve runtime contracts ​

Keep public APIs, package/class relationships, reflection strings, service-provider declarations, JNI bindings, serialization contracts, and framework-discovered names compatible with their callers. Do not rename a method only because it is private: reflection can still reference it.

Codefend does not analyze Java syntax or bytecode. Matching text in strings and comments can also change. Compile and run the complete generated application; review current mapping and replacement limitations before adopting custom pools or a larger input tree.

Add Codefend to your workflow ​

Add Codefend before your Java compilation or source-run step. In a Maven or Gradle application, retain the existing project configuration and make its build and tests consume a generated-source copy with matching package paths.

sh
codefend --obfuscate
java release/Main.java

In CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.