Java version of main.cpp