Java detect windows version

Как проверить версию JDK, установленную на вашем компьютере

В терминале мы можем использовать “javac-версию” для проверки версии JDK и `java-версию` для проверки версии JRE.

Комплект для разработки Java (JDK) содержит инструменты для разработки Java, а Среда выполнения Java (JRE) содержит JVM для преобразования байтового кода .class для машинного кода и его выполнения, короче говоря, JRE запускает программу Java.

Проверьте версию JDK Мы можем использовать java-версию чтобы узнать версию установленного JDK. В приведенном ниже примере версия JDK равна

Проверьте версию JRE Мы можем использовать java-версию , чтобы узнать версию установленной JRE. В приведенном ниже примере версия JRE равна 1.8.0_252

$ java -version openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~19.10-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Версии JDK и JRE могут отличаться на одном компьютере. На одном компьютере разрешено несколько версий JDK и JRE; лучше выяснить, какая версия настроена в системном пути к классам для запуска или компиляции программы Java.

1. Где установлен JDK?

JDK также содержит JRE для запуска программы Java.

Читайте также:  What is static inner class in java

1.1 В Ubuntu или Linux мы можем использовать какой javac , чтобы узнать, где установлен JDK.

$ which javac /usr/bin/javac $ ls -lsah /usr/bin/javac /usr/bin/javac -> /etc/alternatives/javac $ ls -lsah /etc/alternatives/javac /etc/alternatives/javac -> /usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/bin/javac $ cd /usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/bin/ $ ./javac -version javac 11.0.7

В приведенном выше примере JDK установлен по адресу /usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/ .

1.2 В Windows мы можем использовать где javac , чтобы узнать, где установлен JDK.

Microsoft Windows [Version 10.0.18362.900] (c) 2019 Microsoft Corporation. All rights reserved. C:\Users\mkyong>which javac 'which' is not recognized as an internal or external command, operable program or batch file. C:\Users\mkyong>where javac C:\opt\jdk-11.0.1\bin\javac.exe

Нужен ли мне JDK или JRE? Для конечных пользователей им необходимо установить JRE для запуска программы Java, а JDK предназначен для разработчиков. В производственной среде команде развертывания необходимо установить JRE только для запуска программы Java. Однако разработчики часто запрашивают установку JDK вместо автономной JRE на рабочем сервере, поскольку JDK содержит JRE, а также дополнительные инструменты для мониторинга и отладки запущенной Java-программы.

Рекомендации

Читайте ещё по теме:

Источник

Detect OS Name and Version Java

Java is Platform independent and can run everywhere. Knowing this, it can be worth knowing in what operation system the application is running. To detect the current operation system, you can use the OSInfo class below, which retrieves the information from the system properties and returns an OS enum that holds the name and version of the operating system the application is running on.

Detect Operating System

We can get the current operation system name by calling System.getProperty(«os.name»); . We evaluate this value and appropriately map it to the correct os. Finally we add the version number of the os by calling the System.getProperty(«os.version»); .

package com.memorynotfound.file; import java.io.IOException; import java.util.Locale; public class OSInfo < public enum OS < WINDOWS, UNIX, POSIX_UNIX, MAC, OTHER; private String version; public String getVersion() < return version; >public void setVersion(String version) < this.version = version; >> private static OS os = OS.OTHER; static < try < String osName = System.getProperty("os.name"); if (osName == null) < throw new IOException("os.name not found"); >osName = osName.toLowerCase(Locale.ENGLISH); if (osName.contains("windows")) < os = OS.WINDOWS; >else if (osName.contains("linux") || osName.contains("mpe/ix") || osName.contains("freebsd") || osName.contains("irix") || osName.contains("digital unix") || osName.contains("unix")) < os = OS.UNIX; >else if (osName.contains("mac os")) < os = OS.MAC; >else if (osName.contains("sun os") || osName.contains("sunos") || osName.contains("solaris")) < os = OS.POSIX_UNIX; >else if (osName.contains("hp-ux") || osName.contains("aix")) < os = OS.POSIX_UNIX; >else < os = OS.OTHER; >> catch (Exception ex) < os = OS.OTHER; >finally < os.setVersion(System.getProperty("os.version")); >> public static OS getOs() < return os; >>

Detecting the current os the application is running on.

package com.memorynotfound.file; public class DetectOS < public static void main(String. args)< System.out.println("OS: " + OSInfo.getOs()); System.out.println("OS version: " + OSInfo.getOs().getVersion()); System.out.println("Is mac? " + OSInfo.OS.MAC.equals(OSInfo.getOs())); >>
OS: MAC OS version: 10.10.5 Is mac? true

References

Источник

Java — detect operating system name (Windows, Linux, macOS)

Payne

In this short article, we would like to show how to detect used operating system in Java application.

String osName = System.getProperty("os.name"); // Windows 10 // Linux // Mac OS X // etc.
// import org.apache.commons.lang3.SystemUtils; boolean isWindows = SystemUtils.IS_OS_WINDOWS; boolean isLinux = SystemUtils.IS_OS_LINUX; boolean isMac = SystemUtils.IS_OS_MAC;

Where: SystemUtils should be attached as dependecy (Maven dependency is avaialble here).

Operating system name detection

In this section, you can find information what os.name property should return on specific operating system.

Operating system type detection

Knowledge about operating system name may be requred when some operating system uses specific configuration or resources. In this section we would like to show different aproaches to detect operating system type.

1. Apache Commons library

Using this approach it is required to attach commons-lang3 library provided by Apache Software Foundation.

Usage example (it was run under Windows 10):

import org.apache.commons.lang3.SystemUtils; public class Program < public static void main(String[] args) < // Most popular usage: System.out.println(SystemUtils.IS_OS_WINDOWS); // true System.out.println(SystemUtils.IS_OS_LINUX); // false System.out.println(SystemUtils.IS_OS_MAC); // false // Less popular usage: System.out.println(SystemUtils.IS_OS_SOLARIS); // false System.out.println(SystemUtils.IS_OS_SUN_OS); // false >>

Hint:

Using commons-lang3 library we are able to detect more precised OS version, e.g. SystemUtils.IS_OS_WINDOWS_8 , SystemUtils.IS_OS_WINDOWS_10 , etc.

true false false false false
  org.apache.commons commons-lang3 3.12.0 

2. Custom solution

In this section, you can find simple custom solution that lets to detect Windows , Linux or Mac operating system. For more precised detection it is recommended to use commons-lang3 library.

Usage example (it was run under Windows 10):

OS code: Windows OS name: Windows 10 is Windows: true is Linux: false is Mac: false

Example SystemUtils.java file:

public class SystemUtils < /** Returns OS name, e.g. Windows 10, Linux, Mac OS X, etc. */ public static final String OS_NAME = getOSName(); private static final String OS_TEXT = OS_NAME.toLowerCase(); public static final boolean IS_OS_WINDOWS = OS_TEXT.startsWith("windows"); public static final boolean IS_OS_LINUX = OS_TEXT.startsWith("linux"); public static final boolean IS_OS_MAC = OS_TEXT.startsWith("mac"); /** Returns OS code, e.g. Windows, Linux, Mac, Unknown */ public static final String OS_CODE = getOSCode(); private SystemUtils() < // Nothing here . >private static String getOSName() < return System.getProperty("os.name"); >private static String getOSCode() < if (IS_OS_WINDOWS) < return "Windows"; >if (IS_OS_LINUX) < return "Linux"; >if (IS_OS_MAC) < return "Mac"; >return "Unknown"; > >

References

Alternative titles

  1. Java — check operating system name (Windows, Linux or Mac OS)
  2. Java — detect OS name
  3. Java — check OS name (Windows, Linux or Mac OS)
  4. Java — get operating system name
  5. Java — get operating system name (Windows, Linux or Mac OS)
  6. Java — get OS name
  7. Java — get OS name (Windows, Linux or Mac OS)
  8. Java — detect OS type
  9. Java — get OS type
  10. Java — check OS type
  11. Java — determine operating system

Источник

Java Technique for Identifying Operating System

To obtain details about the operating system, you can utilize the following properties: the name of the operating system, its architecture, and the version. To meet your specific needs, you may find the combination of one or more of these useful. A suitable choice for your requirements would be the mentioned property. The Java System Properties Tutorial provides additional information and is accessible across all platforms.

How to determine the Operating System using Java?

To guarantee the loading of the correct JNI lib from the Java runtime, one must attempt to load any of them using System.loadLibrary() and handle exceptions until a native library is successfully loaded. Any other methods used to identify the underlying OS are merely heuristic approaches.

Utilize System.getProperty(«os.name») as there appears to be no alternative method, and disregard any concerns about setProperty since it is unlikely that anyone would set it to foo or bar .

How to determine the Operating System using Java?, I want to find out the OS in a java program so that I can load the correct native library. I know that using System.getProperty(«os.name») is an option, but I would prefer not to use it because it

How to get operating system in Java

System.getProperty("os.name"); System.getProperty("os.version"); System.getProperty("os.arch"); 

Utilize the utility class that I composed by duplicating the subsequent class in your project from the given link: https://github.com/aurbroszniowski/os-platform-finder/blob/master/src/main/java/org/jsoftbiz/utils/OS.java. After that, proceed with the following steps:

import org.jsoftbiz.utils.OS; OS myOS = OS.getOs(); myOS.getPlatformName() myOS.getName() myOS.getVersion() myOS.getArch()

How to find the operating system details using, Just call the os module with const os = require («os»);. then log the constant in the console. You’ll get a whole object, but if you want to see the platform name you can type in console.log (os.platform ());, and make sure you add a pair of parenthesis after os.platform as platform here is a function! Hope this …

Is there any way to find os name using java?

By utilizing System.getProperty() , you can obtain the ensuing characteristics:

  • The MSDTHOT refers to the name of the operating system.
  • The MSDTHOT refers to the architecture of the operating system. os.arch is the code that represents it.
  • This code ( os.version ) pertains to the version of the operating system.

The property you’re seeking is likely os.version , according to your situation. For a comprehensive list of retrievable properties, refer to the javadocs of System.getProperties() .

After conducting a test in Linux Mint, it has been observed that the retrieval of the os.version attribute provides the kernel version instead of the distribution version.

Upon discovering this post, it appears that there is no dependable method available for determining the Linux distribution being used via the Java API.

In case you are operating on Linux, you have an alternative to execute any of these system commands from Java. However, you will need to extract the distribution by using grep or parsing.

System.out.println("\nName of the OS: " + System.getProperty("os.name")); System.out.println("Version of the OS: " + System.getProperty("os.version")); System.out.println("Architecture of the OS: " + System.getProperty("os.arch")); 

The output displayed for Windows is as follows after editing:

Name of the OS: Windows XP Version of the OS: 5.1 Architecture of the OS: x86 

To obtain information, you may query certain system properties. For more information, refer to the tutorial. Using one, two, or all of these three properties in combination should assist you.

System.getProperty("os.arch") System.getProperty("os.name") System.getProperty("os.version") 

How to Detect workstation/System Screen Lock/unlock, Unfortunately there is no pure java solution for this task. The only possible way is to use Java + JNI + call to system dll files. This perhaps worked once but under Windows 10 does not work. The purpose of the program is allow the admin at assign who can unlock the computer but it also has logging capability.

How can I check the bitness of my OS using Java? (J2SE, not os.arch)

For additional details, refer to the Java System Properties Tutorial to ensure accessibility across various platforms.

When a 32 bit JVM is running on a 64 bit Windows platform, the latter will provide false information about the environment to ensure that 32 bit programs run smoothly on a 64 bit OS. It’s important to note that this behavior is not exclusive to JVMs and can be observed with any 32 bit process. For further details, refer to the MSDN article on WOW64.

Due to WOW64, when a 32 bit JVM invokes System.getProperty(«os.arch») , the result obtained is «x86». However, to retrieve the actual architecture of the underlying Windows OS, the following logic can be utilized.

String arch = System.getenv("PROCESSOR_ARCHITECTURE"); String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432"); String realArch = arch != null && arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64") ? "64" : "32"; 

HOWTO: Detect Process Bitness

The reason behind x86 being consistently returned by %processor_architecture% instead of AMD64.

Determine the architecture of the current Windows version, either 32 bit or 64 bit.

Be cautious of relying on the solution that considers os.arch to be the bitness of the OS since it is actually the bitness of the JRE as explained in this link: http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html.

Platform specificity is inevitable in this case. Refer to the final post on this page, where a platform-specific solution is provided.

The os.name property provides the name of the operating system in use while the os.version property provides its version.

Operating system — How do I check CPU and Memory, That’s the method I was referring to is from that OperatingSystemMXBean. JMX, The MXBeans (ThreadMXBean, etc) provided will give you Memory and CPU usages. OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) …

Источник

Оцените статью