Hopefully, you can't do it from within java code silently, as that would be a rather gaping security issue if an app can just upgrade its own access rights like that. You have 3 broad options. 2 of which are based on the notion of:
Make the app pop up a prompt, asking the user to allow it (if they are admin) or to provide admin credentials and then allow it.
- Java is platform agnostic, but just about every platform imaginable does have 'users with different privilege levels' as a concept baked into it, and they all have the concept of 'any given app runs as a given user' baked into it. Thus, one can imagine a java method that you can call to ask to 'upgrade' your privilege level, which should cause the method to cause a popup to appear, on any OS that supports this, failing with an exception if the user cancels the dialog, or the platform does not support this idea.
- Given that you're specifically caring about Windows11 and nothing else, a 'simpler' more direct method that works on windows and pops up a UAC prompt to elevate this app's access, and fails on any other platform.
- Instead of writing code, just run the app with the right access level in the first place. Some OSes do not let apps escalate like this; an app starts under a given user and cannot be modified. Hence, this concept (don't switch users mid-app, start the app the right way immediately) works on more platforms.
As far as I know, option 1 simply does not exist. It could exist, and I bet one would find it in the java.awt.Desktop class if it did. Option 2 could be done with a DLL and native bridging; you may want to search the web, such a thing could exist too, but I'm not aware of one either.
That leaves option 3. Which is easy enough, but it means you have to do it / you have to write a manual so your users know how to do it / you have to write an installer that sets up scripts or whatnot to do this.
A few strategies are available to accomplish this. The 2 most obvious ones:
Shortcut
Right click on the desktop, create a new shortcut. The command you're looking for is something like C:\Program Files\Java\JDK1.11\bin\java.exe -jar C:\myapp\myapp.jar - replacing both of those paths with where your JDK is actually at, and where your app is actually at. In the 'advanced' tab of this 'create shortcut' dialog, you can tick the box with 'run as administrator'. Now run this shortcut and voila - your entire java app now runs as administrator and will be allowed to access that folder.
Run directly
Hit Win+X, pick 'run command line as admin'. Alternatively, just go for windows run (Win+R), type 'cmd', tick the 'run as admin' box, or just hit Windows key, type cmd, right click the 'cmd.exe' search result that pops up, and pick 'run as administrator'. Many ways to end up with an admin-level command line box.
Once you have that, just run C:\Program Files\Java\JDK1.11\bin\java.exe -jar C:\myapp\myapp.jar - replacing the paths as appropriate. The java you start will inherit the admin rights that the cmd box is running under.