1 package snow.utils; 2 3 import java.io.*; 4 import java.util.*; 5 import snow.utils.ProcessUtils; 6 7 9 public final class SysUtils 10 { 11 12 private SysUtils() 13 { 14 } 15 16 18 public static void openFileExplorer(String dir) 19 { 20 21 if(System.getProperty("os.name", "Windows").toLowerCase().indexOf("windows")>=0) 22 { 23 try 24 { 25 String wname = dir.replaceAll("/", "\\\\").trim(); 26 if(wname.indexOf(' ')>0) 27 { 28 ProcessUtils.executeProcessAndGobble(Arrays.asList("explorer.exe" ,"\""+wname+"\""), "open explorer"); 30 } 31 else 32 { 33 ProcessUtils.executeProcessAndGobble(Arrays.asList("explorer.exe" ,"/e,"+wname), "open explorer"); 35 } 36 } 37 catch(Exception e) { e.printStackTrace(); } 38 } 39 else if(System.getProperty("os.name", "linux").toLowerCase().indexOf("linux")>=0) 40 { 41 try 42 { 43 ProcessUtils.executeProcessAndGobble(Arrays.asList(System.getProperty("os.filebrowser", "konqueror"),dir), "open explorer"); 45 } 46 catch(Exception e) { e.printStackTrace(); } 47 } 48 } 49 50 public static void openBrowser(String url) throws Exception 51 { 52 if(is_Windows_OS()) 53 { 54 Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+url); 56 ProcessUtils.drainProcess(p, false); 57 } 58 else 59 { 60 Process p = Runtime.getRuntime().exec(""+url); 61 ProcessUtils.drainProcess(p, false); 62 } 64 } 65 66 69 public static boolean setReadWriteAttribs(final File f, String windowsGroup) throws Exception 70 { 71 if(is_Windows_OS()) 72 { 73 System.out.println("setting read-write-access for "+f); 75 String reply = null; 76 if(f.isDirectory()) 77 { 78 reply = ProcessUtils.readWholeProcessStack(Arrays.asList("cmd", "/c", "CACLS", ""+f.getName(), 79 "/t", "/e", "/g", windowsGroup+":f" ), 82 f.getParentFile(), 83 Arrays.asList("y\r\n"), 84 true ); 86 System.out.println("ok ? "+reply); 87 } 88 else 89 { 90 reply = ProcessUtils.readWholeProcessStack(Arrays.asList("cmd", "/c", "CACLS", ""+f.getName(), 91 "/e", "/g", windowsGroup+":f"), 94 f.getParentFile(), 95 96 Arrays.asList("y\r\n"), true 98 ); 99 System.out.println("set. "+reply); 100 } 101 102 108 } 109 else 110 { 111 try 112 { 113 if(f.isDirectory()) 114 { 115 String reply = ProcessUtils.readWholeProcessStack(Arrays.asList( 116 "/bin/sh", "-c", "chmod -R 777 "+f.getName()), f.getParentFile()); 117 if(reply==null || reply.trim().length()>0) 118 { 119 System.out.println("Cannot set read write file access for folder "+f+":\n"+reply); 120 return false; 121 } 122 return true; 123 } 124 else 125 { 126 String reply = ProcessUtils.readWholeProcessStack(Arrays.asList( 127 "/bin/sh", "-c", "chmod 777 "+f.getName()), f.getParentFile()); 128 if(reply==null || reply.trim().length()>0) 129 { 130 System.out.println("Cannot set read write file access for file "+f+":\n"+reply); 131 return false; 132 } 133 return true; 134 } 135 } 136 catch(Exception ex) 137 { 138 ex.printStackTrace(); 139 return false; 140 } 141 } 142 return true; 143 } 144 145 public static boolean is_Windows_OS() 146 { 147 if(System.getProperty("os.name", "windows").toLowerCase().indexOf("win")>=0) return true; 148 149 return false; 150 151 } 152 153 157 158 159 } | Popular Tags |