Note: All the similar questions are answering this from the perspective of the shell (e.g. Is javac
available? java -version
or which java
). I'm looking explicitly for the perspective of the currently running JVM.
There are programs that require to be run from within a JDK's JRE, not "just" a JRE. I'm wondering if there's a simple way to find out what my currently running program is executed in.
I'm looking for a generic way to figure this out, rather than an analysis of the program in question and duplicating its use of JDK features. That's why I'd prefer not to execute an external process testing if javac
is on the path, if possible. I'm rather looking for some code that will run within a JDK, but fail within a JRE.
It could be Class.forName
with a class that's only available in a JDK. It could be a system property. Or anything else.
If I have to execute an external process with javac: So be it. But I'd prefer something simpler and generic.
Clarification from deep down in the comments:
From time to time I'm running into this problem with Liferay, which requires to be run from within a JDK's JRE. I was entertaining the thought to just deploy another plugin that provides a userfriendly error message if run without a JDK available. I shy away from analyzing which code is the one failing in a JRE-only environment, and I don't want to modify Liferay's code, rather add my own plugin to do the analysis and warn.
javax.tools.ToolProvider.getSystemJavaCompiler()
will return null
if no compiler is available, and a JavaCompiler
if it is.
Technically it just tells you if the compiler is available of course, but that in most scenarios will imply the existence of the JDK.