简体   繁体   中英

Can java.awt.Desktop open a file with administrator privilege?

I tried to open a file using Desktop class, this is my code:

try{
    //filePath is an instance String variable
    desktop.open(new File(filePath));
}catch(IOException ex){
    System.out.println(ex.getMessage());
}

But the problem here the file must be opened with administrator then it'll be run.

For example; if we have this command

netsh wlan start hostednetwork

I saved it in netsh.bat file, when I am opening this file, it'll be run automatically, but unfortunately; windows needs administrator privilege to run this command.

I think opening this file ( neths.bat ) as administrator solves the problem.

But How to do it? , is the question here.

Thanks

Desktop.getDesktop().open(file);

SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file, or it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess

Ex.

if(file.canRead()) {
    Desktop.getDesktop().open(file);
}

Or

ProcessBuilder pb = new ProcessBuilder("netsh.bat");
pb.directory(new File(currentDir + "\\com\\project"));
Process p = pb.start();
int status = p.waitFor();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM