Skip to main content
1 of 2
Peter Taylor
  • 43.4k
  • 4
  • 72
  • 179

I'm not sure what my score is, because it depends on what precisely you consider to be a lexical token, but I'm trying to abuse that counting system as much as possible with a long string...

It also depends on whether you count this as identifying 7 different versions or 16... (It could trivially be extended up to 190).

class V extends ClassLoader { public static void main(String[]args) { for(byte b=60;;) try { byte[]buf="\u00ca\u00fe\u00ba\u00be\u0000\u0000\u00002\u0000\u0005\u0007\u0000\u0003\u0007\u0000\u0004\u0001\u0000\u0001A\u0001\u0000\u0010java/lang/Object\u0006\u0000\u0000\u0001\u0000\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000".getBytes("ISO-8859-1"); buf[7]=b--; new V().defineClass(buf,0,53); System.out.println(b-43); break; } catch(Throwable t){} } } 

It works by attempting to define an interface in a custom classloader with descending major version numbers of the class format. The first one which doesn't throw a java.lang.UnsupportedClassVersionError corresponds to the VM's version.

Peter Taylor
  • 43.4k
  • 4
  • 72
  • 179