Exception in thread "main" java.io.IOException: Cannot run program "attrib": error=2, No such file or directory
In fact, the key to the question is what operating system you are using, MacOS and Windows have very different commands.
The commands provided online are all based on Windows, which is why your operation is invalid.
If you want to hide the folder on Windows, you can use the following code,
Runtime.getRuntime().exec("attrib +H " + dir.getAbsolutePath());
If you want to hide the folder on MacOS, you can use the following code,
Runtime.getRuntime().exec("chflags hidden " + dir.getAbsolutePath());