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?

CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

2 Answers2

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
}
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 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. – David Z Oct 20 '11 at 09:17
  • 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](http://www.java.net/node/688208) - 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... – gilad hoch Oct 10 '12 at 09:02
  • Not working on JDK7x64, are we sure this still works? – spy May 02 '17 at 17:20
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.

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186