{"record":{"id":"256f9d964ac060d2","repo":"MyCATApache/Mycat-Server","slug":"can-t-find-class-resource-file-mapfile-256f9d","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/PartitionByPrefixPattern.java","lineNumber":110,"sourceCode":"\t\t/*\r\n\t\t * fix #1284 这里的统计应该统计Range的nodeIndex的distinct总数\r\n\t\t */\r\n\t\tSet<Integer> distNodeIdxSet = new HashSet<Integer>();\r\n\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\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 = NumberParseUtil.parseLong(pairs[0].trim());\r","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/function/PartitionByPrefixPattern.java#L92-L128","documentation":"PartitionByPrefixPattern.initialize() reads its range map file from the classpath via ClassLoader.getResourceAsStream(mapFile). When the resource is absent (fin == null), it throws this RuntimeException at rule init time. Like PartitionByPattern, the rule fails fast during configuration loading.","triggerScenarios":"init() is invoked on a PartitionByPrefixPattern rule whose <mapFile> property names a resource missing from the classpath, e.g. mapFile=prefix-partition.txt when the file is not in conf/ or on the application classpath.","commonSituations":"Configuring the rule with an absolute OS path instead of a classpath-relative name; forgetting to copy the map file into Mycat's conf directory; the map file exists but under a different name/case than mapFile specifies; packaged archive lacks the resource.","solutions":["Copy the prefix pattern map file into the classpath (Mycat conf directory or src/main/resources) using the exact name referenced by mapFile.","Check the mapFile property for typos, leading slashes, or absolute paths and make it a classpath-relative resource name.","Rebuild the deployment artifact so the resource is packaged, then restart Mycat.","As a code change, add a filesystem fallback (FileInputStream) when the classpath lookup fails."],"exampleFix":"// before\n<property name=\"mapFile\">/home/user/prefix-partition.txt</property>\n// after: file placed in conf/ (classpath)\n<property name=\"mapFile\">prefix-partition.txt</property>","handlingStrategy":"validation","validationCode":"boolean ok = PartitionByPrefixPattern.class.getClassLoader().getResource(mapFile) != null;\nif (!ok) throw new IllegalStateException(\"missing classpath resource for mapFile: \" + mapFile);","typeGuard":null,"tryCatchPattern":"try { rule.init(); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"can't find class resource file\")) { /* correct mapFile and redeploy */ } throw e; }","preventionTips":["Verify the resource loads in a unit test that calls init() before deploying.","Keep mapFile names consistent between schema.xml and the resources directory.","Never use absolute filesystem paths in mapFile; place files on the classpath."],"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"}