Anyone have experience using sun.misc.Unsafe ? I tried using it today and it’s giving an error when i run it.
import sun.misc.*;
public class UnsafeTester {
private static final Unsafe unsafe = Unsafe.getUnsafe();
public static void main(String[] args) {
long ptr = unsafe.allocateMemory(512);
try{
unsafe.putChar(ptr,'a');
unsafe.putChar(ptr,'b');
System.out.println("Unsafe: " + unsafe.toString());
}
finally{
unsafe.freeMemory(ptr);
}
}
}
java.lang.ExceptionInInitializerError
Caused by: java.lang.SecurityException: Unsafe
at sun.misc.Unsafe.getUnsafe(Unsafe.java:68)
at garbage.UnsafeTester.<clinit>(UnsafeTester.java:8)
Exception in thread "main"
I’m not sure exactly how I should go about tackling this one so any help would be great.