1

I need to programmatically move some conf file inside Program Files. Target environment is windows. Here is the example code I have: https://gist.github.com/gandra/911f465217011e2dfec1a8e561c0bb57 but after execution I receive following error:

"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=59066:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\target\classes;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\sahi.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\ant-sahi.jar;C:\Users\draganmijatovic\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\draganmijatovic\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\draganmijatovic\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-core\2.12.0\log4j-core-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-api\2.12.0\log4j-api-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar" MoveFilesDemo
Exception in thread "main" java.nio.file.AccessDeniedException: C:\temp\testfile.txt -> C:\Program Files\testfile.txt
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
    at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
    at java.nio.file.Files.move(Files.java:1395)
    at MoveFilesDemo.main(MoveFilesDemo.java:10)

Process finished with exit code 1

Is there a way to run it as admin or any other option to achieve it? To setup some java permission etc ... Please do not advice me to keep this file in Program Data or repeating that I should not bypass this like in following question: Bypass Windows permission restrictions on program files folder

This is part of some automated testing of some legacy desktop app and need fix to this problem in order to test it. So it is totally acceptable for me to bypass security or any other option which will help me to achieve this without human intervention during execution is welcome.

gandra404
  • 5,727
  • 10
  • 52
  • 76
  • run it as admininistrator... type CMD right click on it and run as administrator. then within the command prompt run ur java – nafas Aug 19 '19 at 14:16
  • Did you actually read the answers to [the question to which you’ve linked](https://stackoverflow.com/questions/3169404/bypass-windows-permission-restrictions-on-program-files-folder)? The *whole point* of that permission restriction is to stop programs—including yours—from accessing system directories unless the user gives them explicit permission. You are not supposed to be able to bypass it. – VGR Aug 19 '19 at 14:17

1 Answers1

-2

You need to change the permission like that :

// changing the file permissions 
            file.setExecutable(true); 
            file.setReadable(true); 
            file.setWritable(true); 
Ajaya Tiwari
  • 115
  • 4