如何使用64位浏览器和64位java插件在64位Linux上获得32位JRE路径

时间:2012-05-11 07:35:27

标签: java linux ubuntu applet 32bit-64bit

我的申请包含三个部分:

  1. 小程序
  2. Java程序(myapp.jar)
  3. JNI图书馆(myjni.so)
  4. 请注意,JNI库是为32位构建的。在32位操作系统上,applet使用java.home属性来获取JRE路径。一旦applet获得JRE路径,它就像这样启动JAR

    JRE-path myapp.jar
    

    现在我需要在64位Linux上运行此应用程序。在这里,我有两个选择:

    1. 为64位构建JNI库。 这是不可能的,因为所有依赖库需要构建64位。 (这是我的约束)
    2. 要求用户安装32位JVM 现在问题是如何获得32位JRE路径,因为java.home属性给出了64位JRE路径。 (因为浏览器和插件是64位)。一种选择是使用update-alternatives –list java命令获取所有JRE安装路径。然后,对于每个安装路径,运行JRE-path -d32 –version命令以查看它是否支持32位JVM
      • 如果它支持32位JVM,请使用该JRE路径启动JAR文件
      • 如果没有java安装支持32位JVM,则显示消息以安装32位JVM
    3. 问题:

      1. 上述解决方案有问题吗? (我需要在Ubuntu,Redhat和OpenSuse上使用此解决方案)
      2. 在64位Linux上获得32 JRE路径是否有更好的解决方案?

1 个答案:

答案 0 :(得分:1)

使用选项1.构建32位和64位JNI库,并根据32位或64位VM加载相关的.so。

您可以使用Sun JDK的sun.arch.data.model系统属性

您可以将com.ibm.vm.bitmode用于IBM websphere VM

或者查找64系统属性中的子串os.arch(基于64位intel的VM上的x86_64 / amd64)

由于您无法构建.so及其所有依赖.a.so文件的64位变体(这实际上是良好的软件配置管理实践),因此以下shell脚本应该努力。如果在调用它结束时脚本以66退出,则没有有效的32位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