Browse Source

Pull request #1573: REPORT-46819 win7下加载j2v8dll之后程序无法退出

Merge in CORE/base-third from ~BJORN/base-third:release/10.0 to release/10.0

* commit '95273a6c6751b8cb97d59a3304928a6328bc2c14':
  代码质量
  代码质量
  REPORT-46819 win7下加载j2v8dll之后程序无法退出
release/10.0
eason 4 years ago
parent
commit
79a7fafe4b
  1. 48
      fine-j2v8/src/main/java/com/eclipsesource/v8/LibraryLoader.java
  2. 1
      fine-j2v8/src/main/java/com/eclipsesource/v8/V8.java

48
fine-j2v8/src/main/java/com/eclipsesource/v8/LibraryLoader.java

@ -15,6 +15,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.management.ManagementFactory;
class LibraryLoader { class LibraryLoader {
@ -23,6 +24,8 @@ class LibraryLoader {
static final String SWT_LIB_DIR = ".j2v8"; static final String SWT_LIB_DIR = ".j2v8";
static final String MINIMUM_VERSION = "6.2";
static { static {
DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$ DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$ SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
@ -193,4 +196,49 @@ class LibraryLoader {
} catch (Throwable e) { } catch (Throwable e) {
} }
} }
//内核版本windows NT6.2以下存在加载dll之后无法正常退出的问题,注册钩子函数退出时杀掉进程
static void checkExceptionVersion() {
String version = System.getProperty("os.version");
if (PlatformDetector.OS.isWindows() && belowMinimumVersion(version)) {
registerExit();
}
}
//低于指定版本,6.2以下
private static boolean belowMinimumVersion(String version) {
return compareVersion(version, MINIMUM_VERSION) < 0;
}
private static int compareVersion(String version1, String version2) {
String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配,不能用".";
String[] versionArray2 = version2.split("\\.");
int idx = 0;
int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
int diff = 0;
while (idx < minLength
&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
++idx;
}
//如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
return diff;
}
//低于指定版本,6.2以下
private static void registerExit() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.split("@")[0];
try {
Runtime.getRuntime().exec("taskkill /F /PID " + pid);
} catch (Exception ex) {
System.exit(0);
}
}
});
}
} }

1
fine-j2v8/src/main/java/com/eclipsesource/v8/V8.java

@ -76,6 +76,7 @@ public class V8 extends V8Object {
try { try {
LibraryLoader.loadLibrary(tmpDirectory); LibraryLoader.loadLibrary(tmpDirectory);
nativeLibraryLoaded = true; nativeLibraryLoaded = true;
LibraryLoader.checkExceptionVersion();
} catch (Error e) { } catch (Error e) {
nativeLoadError = e; nativeLoadError = e;
} catch (Exception e) { } catch (Exception e) {

Loading…
Cancel
Save