11package uwu .narumi .deobfuscator .api .context ;
22
3+ import org .apache .logging .log4j .LogManager ;
4+ import org .apache .logging .log4j .Logger ;
35import org .intellij .lang .annotations .MagicConstant ;
46import org .jetbrains .annotations .Contract ;
57import org .jetbrains .annotations .Nullable ;
68import org .objectweb .asm .ClassWriter ;
9+ import uwu .narumi .deobfuscator .api .environment .JavaEnv ;
10+ import uwu .narumi .deobfuscator .api .environment .JavaInstall ;
11+ import uwu .narumi .deobfuscator .api .execution .SandBox ;
712import uwu .narumi .deobfuscator .api .transformer .Transformer ;
813
914import java .io .IOException ;
1520import java .util .ArrayList ;
1621import java .util .HashSet ;
1722import java .util .List ;
23+ import java .util .Optional ;
1824import java .util .Set ;
1925import java .util .function .Supplier ;
2026
@@ -25,6 +31,7 @@ public record DeobfuscatorOptions(
2531 @ Nullable Path inputJar ,
2632 List <ExternalFile > externalFiles ,
2733 Set <Path > libraries ,
34+ @ Nullable Path rtJarPath ,
2835
2936 @ Nullable Path outputJar ,
3037 @ Nullable Path outputDir ,
@@ -53,11 +60,15 @@ public record ExternalFile(Path path, String pathInJar) {
5360 * Builder for {@link DeobfuscatorOptions}
5461 */
5562 public static class Builder {
63+ private static final Logger LOGGER = LogManager .getLogger ();
64+
5665 // Inputs
5766 @ Nullable
5867 private Path inputJar = null ;
5968 private final List <ExternalFile > externalFiles = new ArrayList <>();
6069 private final Set <Path > libraries = new HashSet <>();
70+ @ Nullable
71+ private Path rtJarPath = null ;
6172
6273 // Outputs
6374 @ Nullable
@@ -161,6 +172,18 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
161172 return this ;
162173 }
163174
175+ /**
176+ * Path to rt.jar from Java 8 binaries. Required for sandbox to work properly.
177+ * Examples:
178+ * - Oracle JDK 8: <code>C:/Program Files/Java/jdk1.8.0_202/jre/lib/rt.jar</code>
179+ * - Eclipse Adoptium JDK 8: <code>C:/Program Files/Eclipse Adoptium/jdk-8.0.462.8-hotspot/jre/lib/rt.jar</code>
180+ */
181+ @ Contract ("_ -> this" )
182+ public DeobfuscatorOptions .Builder rtJarPath (@ Nullable Path rtJarPath ) {
183+ this .rtJarPath = rtJarPath ;
184+ return this ;
185+ }
186+
164187 /**
165188 * Output jar for deobfuscated classes. Automatically filled when input jar is set
166189 */
@@ -255,6 +278,25 @@ public DeobfuscatorOptions.Builder skipFiles() {
255278 return this ;
256279 }
257280
281+ /**
282+ * Try to find rt.jar from Java 8 installation
283+ */
284+ @ Nullable
285+ private Path findRtJarPath () {
286+ Optional <JavaInstall > javaInstall = JavaEnv .getJavaInstalls ().stream ()
287+ .filter (javaInstall1 -> javaInstall1 .version () == 8 )
288+ .findFirst ();
289+
290+ if (javaInstall .isPresent ()) {
291+ JavaInstall install = javaInstall .get ();
292+ Path possibleRtJarPath = install .javaExecutable ().getParent ().getParent ().resolve ("jre" ).resolve ("lib" ).resolve ("rt.jar" );
293+ if (Files .exists (possibleRtJarPath )) {
294+ return possibleRtJarPath ;
295+ }
296+ }
297+ return null ;
298+ }
299+
258300 /**
259301 * Build immutable {@link DeobfuscatorOptions} with options verification
260302 */
@@ -269,12 +311,23 @@ public DeobfuscatorOptions build() {
269311 if (this .outputJar != null && this .outputDir != null ) {
270312 throw new IllegalStateException ("Output jar and output dir cannot be set at the same time" );
271313 }
314+ // Try to auto-detect rt.jar path
315+ if (this .rtJarPath == null ) {
316+ Path rtJar = findRtJarPath ();
317+ if (rtJar != null ) {
318+ LOGGER .info ("Auto-detected rt.jar path: {}" , rtJar );
319+ this .rtJarPath = rtJar ;
320+ } else {
321+ LOGGER .warn ("Failed to auto-detect rt.jar path. Please provide path to rt.jar from Java 8 binaries, otherwise sandbox will not work." );
322+ }
323+ }
272324
273325 return new DeobfuscatorOptions (
274326 // Input
275327 inputJar ,
276328 externalFiles ,
277329 libraries ,
330+ rtJarPath ,
278331 // Output
279332 outputJar ,
280333 outputDir ,
0 commit comments