code: public File findRootPackage(File directory) throws IOException {
          if (!directory.isDirectory()) {
              throw new IllegalArgumentException("Can't find package inside file. Argument should be valid directory.");
          }
          File[] children = directory.listFiles(new FileFilter() {
              @Override
              public boolean accept(File pathname) {
                  return isValidSourceFileOrDir(pathname);
              }
          });
          if (children != null) {
              List<File> results = new LinkedList<File>();
              for (File it : children) {
                  if (!it.isDirectory()) {
                      // we have file - let's assume we have main project's package
                      results.add(directory);
                      break;
                  } else {
                      File pkg = findRootPackage(it);
                      if (pkg != null) {
                          results.add(pkg);
                      }
                  }
              }
  
              if (results.size() == 1) {
                  return results.get(0);
              } else {
                  return directory;
              }
          }
          return null;
      }
docs: Recursively looks for first nested directory which contains at least one source file
  
  @param directory
  @return