Skip to content

Instantly share code, notes, and snippets.

@gaborgsomogyi
Created April 6, 2023 12:41
Show Gist options
  • Save gaborgsomogyi/23f2aa96528e11b2e079ba84ef8c221f to your computer and use it in GitHub Desktop.
Save gaborgsomogyi/23f2aa96528e11b2e079ba84ef8c221f to your computer and use it in GitHub Desktop.
    @SuppressWarnings("unchecked")
    public static void setEnv(String key, String value) {
        try {
            Map<String, String> env = System.getenv();
            Class<?> cl = env.getClass();
            Field field = cl.getDeclaredField("m");
            field.setAccessible(true);
            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
            writableEnv.put(key, value);
        } catch (Exception e) {
            throw new IllegalStateException("Failed to set environment variable", e);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment