You can set the "sticky" bit on the permissions of a file to make it always execute with the permissions of the owning UID.
So, if your java file was owned by root, and the sticky bit was set, every time it was executed, by any user, it would run as root.
If the file name was /bin/example, you could set this with . . .
chmod 4755 /bin/example