{"record":{"id":"d7467e0047857ea7","repo":"MyCATApache/Mycat-Server","slug":"can-t-find-class-resource-file-mapfile-d7467e","errorCode":null,"errorMessage":"can't find class resource file {mapFile}","messagePattern":"can't find class resource file (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"src/main/java/io/mycat/route/function/PartitionByPattern.java","lineNumber":109,"sourceCode":"\t\tfor(LongRange range : longRongs) {\r\n\t\t\tdistNodeIdxSet.add(range.nodeIndx);\r\n\t\t}\r\n\t\tint nPartition = distNodeIdxSet.size();\r\n\t\treturn nPartition;\r\n\t}\r\n\r\n\tpublic static boolean isNumeric(String str) {\r\n\t\treturn pattern.matcher(str).matches();\r\n\t}\r\n\r\n\tprivate void initialize() {\r\n\t\tBufferedReader in = null;\r\n\t\ttry {\r\n\t\t\t// FileInputStream fin = new FileInputStream(new File(fileMapPath));\r\n\t\t\tInputStream fin = this.getClass().getClassLoader()\r\n\t\t\t\t\t.getResourceAsStream(mapFile);\r\n\t\t\tif (fin == null) {\r\n\t\t\t\tthrow new RuntimeException(\"can't find class resource file \"\r\n\t\t\t\t\t\t+ mapFile);\r\n\t\t\t}\r\n\t\t\tin = new BufferedReader(new InputStreamReader(fin));\r\n\t\t\tLinkedList<LongRange> longRangeList = new LinkedList<LongRange>();\r\n\r\n\t\t\tfor (String line = null; (line = in.readLine()) != null;) {\r\n\t\t\t\tline = line.trim();\r\n\t\t\t\tif (line.startsWith(\"#\") || line.startsWith(\"//\")) {\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\t\t\t\tint ind = line.indexOf('=');\r\n\t\t\t\tif (ind < 0) {\r\n\t\t\t\t\tSystem.out.println(\" warn: bad line int \" + mapFile + \" :\"\r\n\t\t\t\t\t\t\t+ line);\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\t\t\t\t\tString pairs[] = line.substring(0, ind).trim().split(\"-\");\r\n\t\t\t\t\tlong longStart = Long.parseLong(pairs[0].trim());\r","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/function/PartitionByPattern.java#L91-L127","documentation":"PartitionByPattern.initialize() loads the pattern-to-node mapping file from the classpath via ClassLoader.getResourceAsStream(mapFile). If the resource cannot be found on the classpath, it throws this RuntimeException immediately during rule initialization. Mycat throws it eagerly so a broken partition rule fails at startup instead of at query time.","triggerScenarios":"Calling init() on a PartitionByPattern rule whose <mapFile> points to a resource name that is not present on the classpath, e.g. mapFile=partition-pattern.txt when no such file exists in src/main/resources or the conf directory on the classpath.","commonSituations":"The map file was never added to the classpath (only placed on disk next to the config); the mapFile value contains a leading slash or wrong path casing; the file was renamed or omitted when packaging a jar/war; running from an IDE where the resources directory is not marked as a resource root.","solutions":["Place the map file (e.g. partition-pattern.txt) on the classpath, typically in Mycat's conf directory or src/main/resources, and rebuild/redeploy.","Verify the <mapFile> value in the partition rule config exactly matches the classpath resource name (case-sensitive, no leading '/', no absolute filesystem path).","If the file lives on the filesystem instead, either move it into the classpath or modify the rule to fall back to FileInputStream(fileMapPath) as the commented-out code suggests.","Confirm the artifact actually packages the file: unzip the jar/war and check the resource path."],"exampleFix":"// before (rule config)\n<function name=\"pat\" class=\"io.mycat.route.function.PartitionByPattern\">\n  <property name=\"mapFile\">/data/conf/partition-pattern.txt</property>\n</function>\n// after: put partition-pattern.txt on the classpath and reference it relatively\n<function name=\"pat\" class=\"io.mycat.route.function.PartitionByPattern\">\n  <property name=\"mapFile\">partition-pattern.txt</property>\n</function>","handlingStrategy":"validation","validationCode":"boolean mapFileOnClasspath = PartitionByPattern.class.getClassLoader().getResource(mapFile) != null;\nif (!mapFileOnClasspath) throw new IllegalStateException(\"mapFile not on classpath: \" + mapFile);","typeGuard":null,"tryCatchPattern":"try { rule.init(); } catch (RuntimeException e) { if (e.getMessage().contains(\"can't find class resource file\")) { /* fail fast: fix mapFile/classpath before startup */ } throw e; }","preventionTips":["Always ship the map file in the conf directory or src/main/resources next to the rule config.","Use classpath-relative, lowercase, extension-included resource names in mapFile.","Smoke-test partition rule initialization in CI so a missing resource fails the build, not production startup."],"tags":["classpath","resource-not-found","sharding-config","startup"],"backgroundTag":"resource-not-found","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}