Java determine if 64 bit

InnoSetup: Detect if Java is 32-bit or 64-bit

They all return True , and ec = 1 , despite the fact that I have a 64-bit java. It seems that Exec and ShellExec return True because they succeed to run java , but they do not track the error code java returns.

ShellExec never waits for the process to complete. However, your second to last command ( Exec(‘java’, ‘-d32 -version’ . ) looks fine. What was in ec after it ran? What was in J32 ?

Pascal Scripting: ShellExec documentation says that: «The Wait parameter specifies whether the function should return immediately or wait until the launched process has termimated or is idle.»

@Gray: thanks, I know about this question. It asks, however, about detecting the JVM version from within the Java program.

2 Answers 2

The Inno Setup help states:

The System32 path returned by the constant maps to the 64-bit System directory by default when used in the [Dirs], [Files], [InstallDelete], [Run], [UninstallDelete], and [UninstallRun] sections. This is because Setup/Uninstall temporarily disables WOW64 file system redirection [external link] when files/directories are accessed by those sections. Elsewhere, System32 and map to the 32-bit System directory, as is normal in a 32-bit process.

So in 64-bit mode in the [Code] section, everything is 32-bit. It will execute 32-bit Java and c:\Windows\System32 points to the WOW64 folder, i.e. the 32-bit version of System32.

Читайте также:  Simple slider

This answer shows how to check Java in the registry instead:

Following that answer, the following code seems to work to check whether 64-bit Java 1.7+ is installed:

[Code] function JavaIsMissing(): Boolean; var javaVersionOutput: AnsiString; begin result := not RegQueryStringValue(HKLM64, 'Software\JavaSoft\Java Runtime Environment', 'CurrentVersion', javaVersionOutput); if not result then result := CompareStr(javaVersionOutput, '1.7') < 0; end; [Run] Filename: "\"; StatusMsg: "Java Runtime Enviroment not installed on your system. Installing. "; Check: JavaIsMissing 

Источник

How to find if JVM is 32 or 64 bit from Java program? Example

You can find JVM bit sizes like 32 bit or 64 bit by using either running java command from the command prompt or by using System.getProperty() from Java program. The question is why do you want to know hardware or platform configuration while writing Java code which is supposed to write once and read anywhere(32 bit, 64 bit, etc)? Yes we don't really need to know whether JVM is 32 bit or 64 bit more often but there are many situations when this matters

Check if JVM is 32 or 64 bit from the Java program:

1. Since in 32 bit JVM maximum heap size in Java can not be more than 4GB (theoretically) if you can get JVM version from a script like running java command you can have a different memory configuration for your application. Also, if there are any specific JVM options which only applicable to 64 bit JVM than you can set those.

2. If your Java application is using native libraries then you certainly want to know whether Java running on the host is 32 bit or 64 bit because the native library may have different versions loaded for 32 bit or 64-bit architectures.

I am sure there could be some more practical reasons when you like to find JVM bit size or whether JVM is 64 bit or not

And, if you are serious about improving your advanced JVM skill and learn things like taking and analyzing heap dumps then you can also see these advanced courses for Java programmers to learn more about Performance and Memory managemen t including troubleshooting memory leaks in Java.

How to check if JVM is 32 or 64 bit in host

As I said earlier there are two different approaches either using Java system property like " sun.arch.data.model " or " os.arch " or by running java command from the script and checking its output for certain characters to identify whether JVM is 64 bit or not. let's see an example of different ways to find if JVM is 32 bit or 64 bit:

1. By using System property sun.arch.data.model :

You can find whether your JVM is 32 bit or 64 bit by calling System.getProperty("sun.arch.data.model") at least on Sun's hotspot JVM. I don't expect this to be run on any other Vendor-specific JVM but since most of the programmer or project uses Sun's hotspot JVM. this is a handy option.

For 32 bit JVM " sun.arch.data.model " will be 32 and for 64 bit JVM, this would be 64. here is an example:

2. By using System.getProperty(" os.arch ")

" os.arch " is another System property that you can use to find whether installed JRE or JVM is 32 bit or 64 bit. by name, it sounds that it will return the operating system arch but you can still give it a try. Though I haven't tested on all different JVM, I have read that it can indeed return JVM Bitness.

If you try this on a 64-bit machine and 32 bit JVM combination then please let us know what does it return. here is what it returns in the case of 32 bit JVM:

3. java -d64 -version

4. java -version

Plain old java -version reveals information about JVM bitness only if installed JRE is 64 bit, in case

of 32 bit JVM it doesn't provide any information related to architecture but in the case of 64 bit JVM it

That’s all on how to find if JVM is 32 bit or 64 bit from Java program and command prompt. As I said it's particularly useful if you have a dependency on native libraries which have a different build for 32 bit or 64-bit architecture. Let me know if you have some more ways to find JVM is 64 bit or not, you can also share on which scenario you need to know JVM bitness.

9 comments :

I wanted know whether my JVM is 64 bit or not and found your tips very relevant. I don't require check if JVM is 32 bit but if any JVM is 64 bit than we do load native .so file from separate location.

Calling System.getProperty("os.arch") on a 64-bit Linux system (OpenSUSE 11.4) with a 32-bit JVM returns: i386

I used Java(TM) SE Runtime Environment (build 1.6.0_22-b04)

Hi, We have several JDK and JRE installed on Solaris Sparc box, I want to find out which installed JDK or JRE is 64 bit ? Can you please share command to do that ? Also is it possible to run 32 bit JVM in 64 bit machine i.e. if My Solaris sparc box is 64 bit but JVM is 32 bit can I execute Java program there ?

One of the interesting point to know relate to 32 bit or 64 bit installation is that it varies on different environment e.g In windows and Linux you have separate installation for 32 bit and 64 bit JVM but on Solaris machine e.g. Sparc, one JVM installation represent both 32 bit and 64 bit JVM and until specified by -d32 and -d64, they by default run on 32 bit data model. Which means you may not able to assign more than 3.5GB of heap, despite running 64 bit JVM in 64 bit machine. Solution is use -d64 JVM option to run JVM on 64 bit data model.

How do i solve this error:
Could not create the java virtual machine
A fatal exception occurred. program will exit

I got "JVM Bit size: x86" for os.arch and "32bit" for sun.arch.data.model on my intel machine with 64 bit windows 7 os.

Just change the JRE to JDK 1.7.0_17, you will get the 64 bit JVM.

A nice tutorial on
JVM Architecture Specification Basic The Heap Area Introduction, teach about the JVM Heap Area in details
http://www.youtube.com/watch?v=c-A7ZzxjWUI

JVM Architecture Specification Basic The Method Area explained, teach about the JVM method area
http://www.youtube.com/watch?v=a5GzF2fSSCE

As this question is already answered and explained by Java geeks , I just want to add little extra in it - generally people prefer to switch over 64 but JVM when the existing heap space (1.5 to 1.8 M varies OS to OS) size provided by 32 bit JVM is not suffice for their application , but these days it is very common that people are migrating to 64 bit without understanding the requirement of applications.

Источник

How can I check if I am running 32bit or 64bit java in C#

It isn't clear how you are going to identify which java.exe you are using - a single machine can have many installed. You may have a specific path, or you may need to either use the JAVA_HOME environment variable, or search PATH , or do a combination of both and give priority to one or the other depending on your requirements.

Once you've got your path to java.exe you can use the technique from Kris Stanton on MSDN (which I will repeat here, but is currently linked at MSDN > "Exploring pe file headers using managed code"):

public enum MachineType < Native = 0, I586 = 0x014c, Itanium = 0x0200, x64 = 0x8664 >public static MachineType GetMachineType(string fileName) < // dos header is 64 bytes // PE header address is 4 bytes const int PE_PTR_OFFSET = 60; const int MACHINE_OFFSET = 4; byte[] data = new byte[4096]; using (Stream stm = new FileStream(fileName, FileMode.Open, FileAccess.Read)) < stm.Read(data, 0, 4096); >int PE_HDR_ADDR = BitConverter.ToInt32(data, PE_PTR_OFFSET); int machineUint = BitConverter.ToUInt16(data, PE_HDR_ADDR + MACHINE_OFFSET); return (MachineType)machineUint; > 

To find java.exe on the %PATH% variable, you can call FindOnPath("java.exe") :

public static String FindOnPath(string exeName) < foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';')) < string path = test.Trim(); if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exeName))) return Path.GetFullPath(path); >return null; > 

On my machine, the following code

static void Main(string[] args)

writes the following output:

C:\ProgramData\Oracle\Java\javapath\java.exe x64 

Источник

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