user345794
user345794

Reputation: 191

How to get 32 bit JRE path on 64 bit Linux with 64 bit browser and 64 bit java plugin

My application consists of three components:

  1. Applet
  2. Java program (myapp.jar)
  3. JNI library (myjni.so)

Please note that JNI library is built for 32 bit. On 32 bit OS, applet uses java.home property to get the JRE path. Once applet gets JRE path, it launches JAR like this

JRE-path myapp.jar

Now I need to run this application on 64 bit Linux. Here I have two options:

  1. Build JNI library for 64 bit. This is not possible as all depended libraries needs to build for 64 bit. ( This is a constraint at my end)
  2. Ask users to install 32 bit JVM Now problem is how to get the 32 bit JRE path as java.home property gives the 64 bit JRE path. ( Because browser and plugins are 64 bit). One option is to use update-alternatives –list java command to get all JRE installations path. Then for each installation path, run JRE-path -d32 –version command to see whether it supports 32 bit JVM
    • If it supports 32 bit JVM, use that JRE path to launch JAR file
    • If none of the java installations support 32 bit JVM, show message to install 32 bit JVM

Question:

  1. Is there any problem for above mentioned solution? (I need to use this solution on Ubuntu, Redhat and OpenSuse)
  2. Is there better solution to get 32 JRE path on 64 bit Linux?

Upvotes: 2

Views: 1930

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94654

Use option 1. Build both a 32bit and 64bit JNI library and load the relevant .so based on being on a 32-bit or 64-bit VM.

You can use the sun.arch.data.model system property for the Sun JDK

You can use the com.ibm.vm.bitmode for the IBM websphere VM

Or look for the substring 64 in the os.arch system property (it's x86_64/amd64 on 64bit intel based VMs)

As you cannot build a 64bit variant of the .so and all it's dependent .a and .so files (this is actually good software configuration management practice), then the following shell script should be a good effort. If at the end of calling it the script exits with 66, then there is no valid 32bit java

#!/bin/bash -p

# attempt to find a 32bit VM
# uses a dummy class file (it's just an empty file)

trap 'rm -f /tmp/testclass$$.class /tmp/jlocations.$$' EXIT HUP

touch /tmp/testclass$$.class

tryj() {
    while read java; do
        errout=$($java -cp /tmp -d32 testclass$$ 2>&1)
        if grep -q 'java.lang.ClassFormatError' <<<$errout; then
            # good VM - run with this
            rm -f /tmp/testclass$$.class /tmp/jlocations.$$
            # echo $java "$@"
            exec $java "$@"
        fi
    done </tmp/jlocations.$$
    return 1
}

# try update-alternatives - debian/ubuntu
update-alternatives --list java > /tmp/jlocations.$$ 2>/dev/null

tryj "$@"

# Try alternatives - redhat
alternatives --list java > /tmp/jlocations.$$ 2>/dev/null

tryj "$@"

# then try locate - generic linux/unix
locate java | grep 'bin/java$' > /tmp/jlocations.$$

tryj "$@"

# if we had no luck, then use find - this will be sloooooooooow
find / -wholename '*/bin/java' >/tmp/jlocations.$$ 2>/dev/null
tryj "$@"

exit 66

Upvotes: 1

Related Questions