User:Higgs1/Minecraft Root Dir

The following Java method allows you to change the minecraft root directory, which is normally located at ~/.minecraft. See this page on how to determine that directory. There are two versions of this code, one intended for Minecraft launchers, and another that runs inside Minecraft.

The code works by modifying the value of the first declared private static java.io.File field in the net.minecraft.client.Minecraft class. This should be more cross version compatible that specifying the name of the field, such as "an" (Minecraft 1.4.6).

From Launcher
The first parameter is the class loader (any sub class of java.lang.ClassLoader is fine) that has already loaded the minecraft jar file. LWJGL doesn't need to be loaded at this point, but it's always a good idea to load all the classes at once. The second parameter is the new minecraft root directory, as a File. import java.io.File; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public static void changeRoot(final ClassLoader classloader, final File root) { try { for (final Field f : classloader.loadClass( "net.minecraft.client.Minecraft").getDeclaredFields) if (Modifier.isPrivate(f.getModifiers)               && Modifier.isStatic(f.getModifiers)                && f.getType == File.class) { AccessibleObject.setAccessible(new Field[] { f }, true); f.set(null, root); break; }   } catch (ClassNotFoundException | IllegalArgumentException        | IllegalAccessException | SecurityException e) {} }

From Same Class Loader
This version will only work from within Minecraft. import java.io.File; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public static void changeRoot(final File root) { try { for (final Field f : Class           .forName("net.minecraft.client.Minecraft").getDeclaredFields) if (Modifier.isPrivate(f.getModifiers)               && Modifier.isStatic(f.getModifiers)                && f.getType == File.class) { AccessibleObject.setAccessible(new Field[] { f }, true); f.set(null, root); break; }   } catch (ClassNotFoundException | IllegalArgumentException        | IllegalAccessException | SecurityException e) {} }