Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to determine whether the current VM is Java SE or Dalvik?

Tags:

java

android

I wish to implement something looks like this:

if(isJavaVirtualMachine()){
    System.out.println("You are running on a JVM");
}else if(isDalvikVirtualMachine()){
    Log.i("env","You are running on an android.");
}

Is it possible? If its impossible by Java itself, can I do it by JNI?

like image 349
Fermat's Little Student Avatar asked Mar 11 '13 19:03

Fermat's Little Student


2 Answers

Use System.getProperty. The property names for Dalvik are documented here

like image 145
ykaganovich Avatar answered Oct 18 '22 12:10

ykaganovich


As ykaganovich pointed out, it should be pretty easy to distinguish between plain Java and Android at runtime. The other problem is that you won't have Android specific classes like Log in a plain Java environment, so you will run into problems at runtime! A possible solution would be to assemble platform specific adapters using reflection, so you pull in the dependencies only at runtime (might be somewhat tricky).

like image 24
Gyro Gearless Avatar answered Oct 18 '22 14:10

Gyro Gearless