[BAEL-4556] How to Check if a Java Program Is Running in 64-Bit JVM or 32-Bit JVM (#10139)

* How to Check if a Java Program Is Running in 64-Bit JVM or 32-Bit JVM

How to Check if a Java Program Is Running in 64-Bit JVM or 32-Bit JVM

* baeldung-articlesBAEL-4556 How can I tell if I'm running in 64-bit JVM or 32-bit JVM

Changes as per review comments .
1.Package Name change
2.Variable change from static to instance
3.Test Method name change

* Update JVMBitVersionUnitTest.java

[BAEL-4556] How to Check if a Java Program Is Running in 64-Bit JVM or 32-Bit JVM

* [BAEL-4556]Added code and Junits for JVMBitVersion

* [BAEL-4556]Removed code and Junits for os.arch property to check JVMBitVersion

Co-authored-by: MeenaGawande <MeenaGawande@users.noreply.github.com>
This commit is contained in:
MeenaGawande 2020-11-07 20:48:40 +05:30 committed by GitHub
parent fa2387c836
commit 42276ed18a
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.jvmbitversion;
import com.sun.jna.Platform;
public class JVMBitVersion {
public String getUsingSystemClass() {
return System.getProperty("sun.arch.data.model") + "-bit";
}
public String getUsingNativeClass() {
if (com.sun.jna.Native.POINTER_SIZE == 8) {
return "64-bit";
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
return "32-bit";
} else
return "unknown";
}
public boolean getUsingPlatformClass() {
return (Platform.is64Bit());
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.jvmbitversion;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.jvmbitversion.JVMBitVersion;
import com.sun.jna.Platform;
public class JVMBitVersionUnitTest {
private JVMBitVersion jvmVersion;
@Before
public void setup() {
jvmVersion = new JVMBitVersion();
}
@Test
public void whenUsingSystemClass_thenOutputIsAsExpected() {
if (System.getProperty("sun.arch.data.model") == "64") {
assertEquals("64-bit", jvmVersion.getUsingSystemClass());
} else if (System.getProperty("sun.arch.data.model") == "32") {
assertEquals("32-bit", jvmVersion.getUsingSystemClass());
}
}
@Test
public void whenUsingNativeClass_thenResultIsAsExpected() {
if (com.sun.jna.Native.POINTER_SIZE == 8) {
assertEquals("64-bit", jvmVersion.getUsingNativeClass());
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
assertEquals("32-bit", jvmVersion.getUsingNativeClass());
}
}
@Test
public void whenUsingPlatformClass_thenResultIsAsExpected() {
if (Platform.is64Bit() == Boolean.TRUE) {
assertEquals(Boolean.TRUE, jvmVersion.getUsingPlatformClass());
} else if (com.sun.jna.Native.POINTER_SIZE == 4) {
assertEquals(Boolean.FALSE, jvmVersion.getUsingPlatformClass());
}
}
}