Repository navigation
Expand file tree
/
Copy pathOSUtils.java
More file actions
138 lines (118 loc) · 4.43 KB
/
Copy pathOSUtils.java
File metadata and controls
138 lines (118 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package autoitx4java;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeMapped;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.VerRsrc.VS_FIXEDFILEINFO;
import com.sun.jna.platform.win32.WinReg;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;
// origin: http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java
public class OSUtils {
private static String osName = null;
private static boolean is64bit = false;
public static String getOsName() {
if (osName == null) {
osName = System.getProperty("os.name");
}
return osName;
}
public static String getDesktopPath() throws Exception {
HWND hwndOwner = null;
int nFolder = Shell32.CSIDL_DESKTOPDIRECTORY;
HANDLE hToken = null;
int dwFlags = Shell32.SHGFP_TYPE_CURRENT;
char[] pszPath = new char[Shell32.MAX_PATH];
int hResult = Shell32.INSTANCE.SHGetFolderPath(hwndOwner, nFolder, hToken,
dwFlags, pszPath);
if (Shell32.S_OK == hResult) {
String path = new String(pszPath);
return (path.substring(0, path.indexOf('\0')));
} else {
throw new Exception("Windows Error: " + hResult);
}
}
static class HANDLE extends PointerType implements NativeMapped {
}
static class HWND extends HANDLE {
}
private static Map<String, Object> OPTIONS = new HashMap<>();
static {
OPTIONS.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
OPTIONS.put(Library.OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
}
static interface Shell32 extends Library {
public static final int MAX_PATH = 260;
// https://sourceforge.net/u/cstrauss/w32api/ci/7805df8efec130f582b131b8c0d75e1b6ce0993b/tree/include/shlobj.h?format=raw
public static final int CSIDL_DESKTOPDIRECTORY = 0x0010;
public static final int SHGFP_TYPE_CURRENT = 0;
public static final int SHGFP_TYPE_DEFAULT = 1;
public static final int S_OK = 0;
static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
Shell32.class, OPTIONS);
/**
* see http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
*
* HRESULT SHGetFolderPath( HWND hwndOwner, int nFolder, HANDLE hToken,
* DWORD dwFlags, LPTSTR pszPath);
*/
public int SHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken,
int dwFlags, char[] pszPath);
}
// https://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
public static void killProcess(String processName) {
String command = String.format((osName.toLowerCase().startsWith("windows"))
? "taskkill.exe /F /IM %s" : "killall %s", processName.trim());
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
// process.redirectErrorStream( true);
BufferedReader stdoutBufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
BufferedReader stderrBufferedReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line = null;
StringBuffer processOutput = new StringBuffer();
while ((line = stdoutBufferedReader.readLine()) != null) {
processOutput.append(line);
}
StringBuffer processError = new StringBuffer();
while ((line = stderrBufferedReader.readLine()) != null) {
processError.append(line);
}
int exitCode = process.waitFor();
// ignore exit code 128: the process "<browser driver>" not found.
if (exitCode != 0 && (exitCode ^ 128) != 0) {
System.out.println("Process exit code: " + exitCode);
if (processOutput.length() > 0) {
System.out.println("<OUTPUT>" + processOutput + "</OUTPUT>");
}
if (processError.length() > 0) {
// e.g.
// The process "chromedriver.exe"
// with PID 5540 could not be terminated.
// Reason: Access is denied.
System.out.println("<ERROR>" + processError + "</ERROR>");
}
}
} catch (Exception e) {
System.err.println("Exception (ignored): " + e.getMessage());
}
}
}