Last active
January 5, 2021 17:41
-
-
Save mschmitt/f7947fdc246c1ccee1804422c7d83b13 to your computer and use it in GitHub Desktop.
Find current JVM, either from java builtin property or from $JAVA_HOME
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find current JVM, either from java builtin property or from $JAVA_HOME | |
$ javac JavaHome.java | |
$ java JavaHome | |
/usr/lib/jvm/java-8-openjdk-amd64/jre | |
(Info: /usr/lib/jvm/java-8-openjdk-amd64/jre from builtin) | |
$ java -jar /tmp/JavaHome.jar 2>/dev/null | |
/usr/lib/jvm/java-8-openjdk-amd64/jre | |
$ jar cfe JavaHome.jar JavaHome JavaHome.class | |
$ java -jar JavaHome.jar # etc. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
public class JavaHome { | |
public static void main(String[] args) { | |
String jh_source; | |
String jh_path; | |
String jh_property = System.getProperty("java.home");; | |
if(System.getenv("JAVA_HOME") == null ){ | |
jh_path = jh_property; | |
jh_source = "builtin"; | |
}else{ | |
jh_path = System.getenv("JAVA_HOME"); | |
jh_source = "environment"; | |
} | |
System.out.printf("%s\n", jh_path); | |
System.err.printf("(Info: %s from %s)\n", jh_path, jh_source); | |
if (!jh_property.equals(jh_path)) { | |
System.err.printf("(Notice: Differs from builtin %s)\n", jh_property); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
-rm JavaHome.class JavaHome.jar | |
javac JavaHome.java | |
jar cfe JavaHome.jar JavaHome JavaHome.class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment