Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java get environment information at run time

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to obtain environmental information when java is running". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to obtain environmental information at run time by java" can help you solve your doubts.

Software engineers, especially those who develop client-side products, such as App, inevitably need to judge the current environment. For example, the client product should judge whether it is Windows/Linux system, x86 or x64, and so on. App needs to determine the version of Android and iOS, and whether the current environment is WIFI or not.

For Java applications, whether Web or desktop applications, you will also encounter the need to determine the current version of JDK, the corresponding operating system of the current application, and so on.

For example, when we do application server cluster management and monitoring a few years ago, we need to judge the CPU and memory utilization of the physical machine to which the application server belongs, operate on server instances, adopt different attach mechanisms for different JVM implementations, and so on. At that time, Sigar was used to obtain these hardware information.

Since the version of All in One is provided, you need to determine the type of operating system to determine whether the Windows loaded with Sigar supports dll files or Linux supports so files.

Of course, it is easy to get the type and version of the operating system, which can be obtained directly through the getProperty of System plus the corresponding name, such as "os.name", "os.arch" and so on. For JVM manufacturers, it can be obtained through "java.vm.vendor", and the whole can be obtained through System.getProperties.

If you are accustomed to using JMX to read the MBean of Platform, you can also get it through JMX. The convenient operation is like this:

OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean ()

System.out.println (os.getName ())

It's all the same in essence.

In addition to this way, what other ways can be obtained? What does open source software do in general?

Let's take a look at how Tomcat works internally.

We found that the internal Tomcat, for the properties obtained through System, are basically used for printing Log and output, for the control within the application, basically do not see the use.

So what does Tomcat do when judging the version? Why not just use this?

First of all, let's see how to do it.

As mentioned in the previous article, Tomcat has made some efforts to deal with memory leaks, such as Hold a piece of memory first (Tomcat and memory leak handling), and PreventionListener to prevent memory leaks, which will first load possible shared class into Common classLoader (Tomcat class loader and inter-application class isolation and sharing, class loader and class conflict).

The implementation looks like this:

/ / Trigger a call to sun.awt.AppContext.getAppContext (). This

/ / will pin the system class loader in memory but that shouldn't

/ / be an issue.

If (appContextProtection & &! JreCompat.isJre8Available ()) {

ImageIO.getCacheDirectory ()

}

/ / Trigger the creation of the AWT (AWT-Windows, AWT-XAWT

/ / etc.) Thread

If (awtThreadProtection & &! JreCompat.isJre9Available ()) {

Java.awt.Toolkit.getDefaultToolkit ()

}

Since when is Available?

Static {

/ / This is Tomcat 8 with a minimum Java version of Java 7. The latest

/ / Java version the optional features require is Java 9.

/ / Look for the highest supported JVM first

If (Jre9Compat.isSupported ()) {

Instance = new Jre9Compat ()

Jre9Available = true

Jre8Available = true

}

Else if (Jre8Compat.isSupported ()) {

Instance = new Jre8Compat ()

Jre9Available = false

Jre8Available = true

} else {

Instance = new JreCompat ()

Jre9Available = false

Jre8Available = false

}

}

Whether it is supported or not is determined directly by loading the class corresponding to a specific version of JDK.

Static {

Method M1 = null

Try {

/ / The class is Java6+...

Class C1 = Class.forName ("javax.net.ssl.SSLParameters")

/ /... but this method is Java8+

M1 = c1.getMethod ("setUseCipherSuitesOrder", boolean.class)

} catch (SecurityException e) {

/ / Should never happen

} catch (NoSuchMethodException e) {

/ / Expected on Java

< 8 } catch (ClassNotFoundException e) { // Should never happen } setUseCipherSuitesOrderMethod = m1; } 通过这种加载 class 的方式,和 我们前面通过 System.property获取,有啥区别呢? 要知道, System 的 Property 是个系统属性,是可配置的,也就是说,谁都可以进行setProperty的操作。如果不巧被别人改了,那你的程序可能就会出现不符合你预期的行为。 而加载类的形式,并不受其他人的影响。是最真实的一线声音。 这样的判断不方式,不仅 Tomcat 自己,其他框架也有在用。我们来看 Spring Boot。 Boot 执行的时候,有时候会判断当前应用是否需要支持 Web, 类似于是否加载了 Spring MVC 这种。 private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext" }; private boolean deduceWebEnvironment() { for (String className : WEB_ENVIRONMENT_CLASSES) { if (!ClassUtils.isPresent(className, null)) { return false; } } return true; } 然后在启动时候,会调用deduceWebEnvironment 方法,从而决定一些执行的逻辑。 还有其他方式么? 如何不想自己手工处理,可以使用一些工具类,比如 Apache 的 commons-lang, 提供了一个 SystemUtils 可以直接。例如判断操作系统和 Java 版本 if (SystemUtils.IS_JAVA_1_7) { System.out.println("Hello 1.7"); } 工具类是怎么做的呢? public static final String JAVA_VERSION = getSystemProperty("java.version");private static String getJavaVersionTrimmed() { if(JAVA_VERSION != null) { for(int i = 0; i < JAVA_VERSION.length(); ++i) { char ch = JAVA_VERSION.charAt(i); if(ch >

= 48 & & ch

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report