找不到 Java 主页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12206262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:02:08  来源:igfitidea点击:

Cannot locate Java home

javalinuxunixjsvc

提问by Naftuli Kay

I'm writing an application that leverages jsvcto start up a Java service as a daemon. I need to use something like jsvcbecause my application utilizes ports under 1024 and yet I'd really like to not run it as root so that created files are owned by another user. I'd also like to keep dependencies and configuration to a minimum so that all the client needs is a JVM and the jsvcbinary installed.

我正在编写一个应用程序,利用该应用程序将jsvcJava 服务作为守护程序启动。我需要使用类似的东西,jsvc因为我的应用程序使用 1024 下的端口,但我真的不想以 root 身份运行它,以便创建的文件归另一个用户所有。我还想将依赖项和配置保持在最低限度,以便所有客户端需要的是 JVM 和jsvc已安装的二进制文件。

However, it seems that jsvchas one major catch; it can't detect the home folder of Java on a given Unix operating system, which is quite frustrating:

然而,它似乎jsvc有一个主要问题;它无法在给定的 Unix 操作系统上检测到 Java 的主文件夹,这很令人沮丧:

$ ./startup.sh
Cannot locate Java home

I have been able to work around the issue on Ubuntu at least by manually setting the JVM home directory:

我至少可以通过手动设置 JVM 主目录来解决 Ubuntu 上的问题:

jsvc ... -home /usr/lib/jvm/default-java/ ...

Is there any way to determine the Java home directory dynamically from a Bash script so I can make this work across most Unixes/Linuxes? I'd be able to sleep much better at night doing something like:

有什么方法可以从 Bash 脚本动态确定 Java 主目录,以便我可以在大多数 Unix/Linux 上进行这项工作?我可以在晚上睡得更好,做如下事情:

JAVA_HOME="$( ... )"

jsvc ... -home "$JAVA_HOME" ...

...rather than hard-coding for each individual operating system. Is there a way that, given a javabinary, I can find the home directory of its JVM/JRE?

...而不是为每个单独的操作系统进行硬编码。有没有办法,给定一个java二进制文件,我可以找到它的 JVM/JRE 的主目录?

采纳答案by Naftuli Kay

Not sure if this works across *nixes, but found this solution:

不确定这是否适用于 *nixes,但找到了这个解决方案:

JAVA_HOME="$( readlink -f "$( which java )" | sed "s:bin/.*$::" )"

I've tested it on Ubuntu and it works, however it does not work for OSX.

我已经在 Ubuntu 上对其进行了测试并且它可以工作,但是它不适用于 OSX。

回答by Jorge Sanchez

My solution was compiling the native linux source as the main jsvc page says in http://commons.apache.org/proper/commons-daemon//jsvc.html

我的解决方案是编译本机 linux 源代码,如http://commons.apache.org/proper/commons-daemon//jsvc.html 中的主要 jsvc 页面所述

Here is my step by step procedure

这是我的分步程序

Download www.fightrice.com/mirrors/apache/commons/daemon/source/commons-daemon-1.0.13-src.tar.gz

下载 www.fightrice.com/mirrors/apache/commons/daemon/source/commons-daemon-1.0.13-src.tar.gz

Once you extract the file then go to ...../commons-daemon-1.0.13-src/src/native/unix

提取文件后,请转到 ...../commons-daemon-1.0.13-src/src/native/unix

in terminal as a root do the following:

在终端中以 root 身份执行以下操作:

$ support/buildconf.sh

$ 支持/buildconf.sh

$ ./configure --with-java=/usr/lib/jvm/default-java

$ ./configure --with-java=/usr/lib/jvm/default-java

$ make

$ make

test generated jsvc binary app

测试生成的 jsvc 二进制应用程序

$ ./jsvc -help

$ ./jsvc -帮助

It works! cleanly.

有用!干净地。

回答by Balaji Boggaram Ramanarayan

One other way is :

另一种方式是:

 type -p java

Expect this to return the correct JAVA installation folder.

期望这会返回正确的 JAVA 安装文件夹。

回答by EthanB

Use dirnameand whichcommands to find Java's bin directory:

使用dirnamewhich命令查找Java的bin目录:

echo `dirname \`which java\``
JAVA_HOME=`dirname \`which java\``

... Only works if Java is already on the $PATH.

... 只有在 Java 已经在$PATH.