The easiest way to protect against unwanted malware is to simply run it in a separate virtual machine. If you are running Linux, boot the virtual machine using KVM or something else, run the program there and write the output file somewhere (for example, via the virtual serial port). Give the virtual machine no access to the network and wipe it every time.
Otherwise, Java has an extensive security and sandbox model, originally designed to isolate applets. However, this is difficult to use correctly, and I would not recommend using it for something like this: spawning a VM is much easier and safer.
source share