I find the solution now, we can use following code to get ModuleId without set the Environment env value in the deployment.template.json.
ModuleClient client = ModuleClient.createFromEnvironment(protocol);
String moduleId = client.getConfig().getModuleId();
System.out.println("moduleId: " + moduleId);