I think you could use something like this,
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
public class Checker {
private static final String HASH_MAIN_CLASS = "Hash of your valid Main class";
public boolean isExecutingValidMainClass() throws IOException{
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
String mainClassName = stack[stack.length-1].getClassName();
String pathToMainClass = mainClassName.replaceAll("\\.", File.separator) + ".class";
InputStream inputStream = AppMain.class.getClassLoader().getResourceAsStream(pathToMainClass);
byte[] mainClassBytes = IOUtils.toByteArray(inputStream);
if(HASH_MAIN_CLASS.equals(byteArrayHash(mainClassBytes))){
return true;
}else{
return false;
}
}
private String byteArrayHash(byte[] byteArray){
//Calculate hash for byte array
//Return hash for byte array
}
}
It is not infallible, but they would have to compile a cheat Main class with the same hash that your Main class. Using an appropriate hash algorithm would be complex using a cheat Main class.