1. 检测jdk版本

 @SuppressWarnings("LoopStatementThatDoesntLoop")
private static int javaVersion0() {
int javaVersion; // Not really a loop
for (;;) {
// Android
if (isAndroid()) {
javaVersion = 6;
break;
} try {
Class.forName("java.time.Clock", false, getClassLoader(Object.class));
javaVersion = 8;
break;
} catch (Exception e) {
// Ignore
} try {
Class.forName("java.util.concurrent.LinkedTransferQueue", false, getClassLoader(BlockingQueue.class));
javaVersion = 7;
break;
} catch (Exception e) {
// Ignore
} javaVersion = 6;
break;
} if (logger.isDebugEnabled()) {
logger.debug("Java version: {}", javaVersion);
}
return javaVersion;
}

2. 检测是否window

    private static boolean isWindows0() {
boolean windows = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US).contains("win");
if (windows) {
logger.debug("Platform: Windows");
}
return windows;
}

3. 检测是否root权限

private static boolean isRoot0() {
if (isWindows()) {
return false;
} String[] ID_COMMANDS = { "/usr/bin/id", "/bin/id", "/usr/xpg4/bin/id", "id"};
Pattern UID_PATTERN = Pattern.compile("^(?:0|[1-9][0-9]*)$");
for (String idCmd: ID_COMMANDS) {
Process p = null;
BufferedReader in = null;
String uid = null;
try {
p = Runtime.getRuntime().exec(new String[] { idCmd, "-u" });
in = new BufferedReader(new InputStreamReader(p.getInputStream(), CharsetUtil.US_ASCII));
uid = in.readLine();
in.close(); for (;;) {
try {
int exitCode = p.waitFor();
if (exitCode != 0) {
uid = null;
}
break;
} catch (InterruptedException e) {
// Ignore
}
}
} catch (Exception e) {
// Failed to run the command.
uid = null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// Ignore
}
}
if (p != null) {
try {
p.destroy();
} catch (Exception e) {
// Android sometimes triggers an ErrnoException.
}
}
} if (uid != null && UID_PATTERN.matcher(uid).matches()) {
logger.debug("UID: {}", uid);
return "0".equals(uid);
}
} logger.debug("Could not determine the current UID using /usr/bin/id; attempting to bind at privileged ports."); Pattern PERMISSION_DENIED = Pattern.compile(".*(?:denied|not.*permitted).*");
for (int i = 1023; i > 0; i --) {
ServerSocket ss = null;
try {
ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(i));
if (logger.isDebugEnabled()) {
logger.debug("UID: 0 (succeded to bind at port {})", i);
}
return true;
} catch (Exception e) {
// Failed to bind.
// Check the error message so that we don't always need to bind 1023 times.
String message = e.getMessage();
if (message == null) {
message = "";
}
message = message.toLowerCase();
if (PERMISSION_DENIED.matcher(message).matches()) {
break;
}
} finally {
if (ss != null) {
try {
ss.close();
} catch (Exception e) {
// Ignore.
}
}
}
} logger.debug("UID: non-root (failed to bind at any privileged ports)");
return false;
}

4.检测最大直接内存

private static long maxDirectMemory0() {
long maxDirectMemory = 0;
try {
// Try to get from sun.misc.VM.maxDirectMemory() which should be most accurate.
Class<?> vmClass = Class.forName("sun.misc.VM", true, getSystemClassLoader());
Method m = vmClass.getDeclaredMethod("maxDirectMemory");
maxDirectMemory = ((Number) m.invoke(null)).longValue();
} catch (Throwable t) {
// Ignore
} if (maxDirectMemory > 0) {
return maxDirectMemory;
} try {
// Now try to get the JVM option (-XX:MaxDirectMemorySize) and parse it.
// Note that we are using reflection because Android doesn't have these classes.
Class<?> mgmtFactoryClass = Class.forName(
"java.lang.management.ManagementFactory", true, getSystemClassLoader());
Class<?> runtimeClass = Class.forName(
"java.lang.management.RuntimeMXBean", true, getSystemClassLoader()); Object runtime = mgmtFactoryClass.getDeclaredMethod("getRuntimeMXBean").invoke(null); @SuppressWarnings("unchecked")
List<String> vmArgs = (List<String>) runtimeClass.getDeclaredMethod("getInputArguments").invoke(runtime);
for (int i = vmArgs.size() - 1; i >= 0; i --) {
Matcher m = MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN.matcher(vmArgs.get(i));
if (!m.matches()) {
continue;
} maxDirectMemory = Long.parseLong(m.group(1));
switch (m.group(2).charAt(0)) {
case 'k': case 'K':
maxDirectMemory *= 1024;
break;
case 'm': case 'M':
maxDirectMemory *= 1024 * 1024;
break;
case 'g': case 'G':
maxDirectMemory *= 1024 * 1024 * 1024;
break;
}
break;
}
} catch (Throwable t) {
// Ignore
} if (maxDirectMemory <= 0) {
maxDirectMemory = Runtime.getRuntime().maxMemory();
logger.debug("maxDirectMemory: {} bytes (maybe)", maxDirectMemory);
} else {
logger.debug("maxDirectMemory: {} bytes", maxDirectMemory);
} return maxDirectMemory;
}

等等

最新文章

  1. vim(vi)常用操作及记忆方法
  2. 浅入浅出EmguCv(二)EmguCv打开指定图片
  3. android学习笔记四
  4. 《Head First 设计模式》ch.1 策略(Strategy)模式
  5. 模板:函数memcpy
  6. 几个模式识别和计算机视觉相关的Matlab工具箱
  7. 查询两个日期(时间)以内的数据,between and 或 and 连&gt;= &lt;=,to_date()
  8. flexbox 伸缩布局盒
  9. KICKSTART无人值守安装
  10. java.lang.reflect.InvocationTargetException
  11. 【BZOJ2002】弹飞绵羊(Link-Cut Tree)
  12. IDEA无法创建类,接口
  13. Linux 搭建 Nginx+PHP-FPM环境
  14. 低成本制作基于OpenWRT的渗透工具
  15. 【持久化框架】Mybatis与Hibernate的详细对比(转发)
  16. 学习windows编程 day4 之 映射模式
  17. 【shell编程】之基础知识-输入/输出和重定向
  18. day4----函数-闭包-装饰器
  19. Redis简单生产者消费者
  20. noip第3课作业

热门文章

  1. LintCode-最大子数组差
  2. mapper.xml中的常用标签
  3. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) D】Bash and a Tough Math Puzzle
  4. JavaScript学习总结(8)——JS实用技巧总结
  5. Chormium线程模型及应用指南
  6. Spring源码分析专题 —— IOC容器启动过程(上篇)
  7. 桌面版chrome调试APP的webview的步骤:
  8. NOIP2015运输计划(二分答案)
  9. 98.TCP通信传输文件
  10. table嵌套table,jquery获取tr个数