Java: how to detect the current java runtime is a JRE or JDK?
Asked Answered
E

2

8

I got a Java application, I want to provide user ability to compile Java source code (Using JavaCompiler interface)

If the user run the application on a JRE, my application should tell user that the no JavaCompiler instance aviable.

so how to detect JDK or JRE in java programe?

Ejaculatory answered 20/10, 2011 at 9:7 Comment(0)
G
11

You can request an implementation of JavaCompiler from ToolProvider. If it returns null, there is no implementation of JavaCompiler available:

JavaCompiler c = ToolProvider.getSystemJavaCompiler();
if (c == null) {
    // JRE
}
Glossography answered 20/10, 2011 at 9:14 Comment(3)
I'm not sure if that works - it seems like the JavaCompiler interface itself is always available, but the distinction of JDK vs. JRE depends on there being an implementation.Rothman
it works great when the JDK is installed properly, and javac can be found on the classpath. but what if i want to allow a user to run it, even if the javac cannot be reached? all i want to know is if i'm running on JDK or JRE... see this link - it can be solved if the user defines the environment variable. but since i have no need for the javac, i don't want to bother the user with this extra configuration overhead...Ness
Not working on JDK7x64, are we sure this still works?Selftaught
U
1

Take a look at stackoverflow thread - How to determine if the Java VM is installed on Windows? , How do I detect which kind of JRE is installed — 32bit vs. 64bit and How can I detect the installed Sun JRE on Windows?

You can use System.getProperties()/System.getProperty() method.

Ultimogeniture answered 20/10, 2011 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.