{"record":{"id":"807af075e4f661e3","repo":"eclipse-vertx/vert.x","slug":"package-structure-does-not-match-directory-structu","errorCode":null,"errorMessage":"Package structure does not match directory structure: ${token} != ${rootDirectory.getName()}","messagePattern":"Package structure does not match directory structure: (.+?) != (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/impl/verticle/JavaSourceContext.java","lineNumber":38,"sourceCode":" *\n * Resolves package name and source path based on a single Java source file.\n *\n * @author Janne Hietam&auml;ki\n */\npublic class JavaSourceContext {\n\n  private final String className;\n  private final File sourceRoot;\n\n  public JavaSourceContext(File file) {\n    String packageName = parsePackage(file);\n    File rootDirectory = file.getParentFile();\n    if (packageName != null) {\n      String[] pathTokens = packageName.split(\"\\\\.\");\n      for(int i = pathTokens.length - 1; i >= 0; i--) {\n        String token = pathTokens[i];\n        if (!token.equals(rootDirectory.getName())) {\n          throw new RuntimeException(\"Package structure does not match directory structure: \" + token + \" != \" + rootDirectory.getName());\n        }\n        rootDirectory = rootDirectory.getParentFile();\n      }\n    }\n    sourceRoot = rootDirectory;\n\n    String fileName = file.getName();\n    String className = fileName.substring(0, fileName.length() - Kind.SOURCE.extension.length());\n    if (packageName != null) {\n      className = packageName + '.' + className;\n    }\n    this.className = className;\n  }\n\n  public File getSourceRoot() {\n    return sourceRoot;\n  }\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/impl/verticle/JavaSourceContext.java#L20-L56","documentation":"Thrown when compiling a Java verticle from source: the verticle's declared package name must match the directory hierarchy under the source root. The compiler walks the package tokens backwards against parent directories, and any mismatch aborts compilation because the resulting class file could not be placed correctly.","triggerScenarios":"Calling Vertx.deployVerticle with a .java file whose `package x.y.z;` declaration does not correspond to its actual directory layout, e.g. the file sits in a directory whose name differs from the last package token.","commonSituations":"Renaming or moving a verticle source file without updating the package declaration; copying a .java file into a flat directory while it still declares a package; IDE-generated sources relocated manually.","solutions":["Move the .java file so its directory path matches the package declaration","Remove or correct the `package` statement in the source file to reflect the real directory structure","Deploy the verticle from the correct source root directory"],"exampleFix":"// before: file at ./MyVerticle.java declaring\npackage com.example.verticles;\npublic class MyVerticle extends AbstractVerticle { ... }\n// after: move file to ./com/example/verticles/MyVerticle.java or delete the package line","handlingStrategy":"validation","validationCode":"Path src = Paths.get(\"MyVerticle.java\");\nString pkg = extractPackageDeclaration(src);\nPath dir = src.getParent();\nString[] tokens = pkg.split(\"\\\\.\");\nPath cur = dir;\nfor (int i = tokens.length - 1; i >= 0; i--) {\n  if (cur == null || !tokens[i].equals(cur.getFileName().toString())) {\n    throw new IllegalStateException(\"Package/dir mismatch at \" + tokens[i]);\n  }\n  cur = cur.getParent();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep sources in src/main/java/<package path>/ and deploy from the source root","Regenerate the package declaration whenever a file is moved","Let the IDE fix package statements after moves instead of editing manually"],"tags":["verticle","java-source-compilation","package-structure"],"backgroundTag":"invalid-argument-value","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}