{"record":{"id":"4a3920f6d2e46de1","repo":"MyCATApache/Mycat-Server","slug":"can-t-find-class-resource-file-mapfile","errorCode":null,"errorMessage":"can't find class resource file ${mapFile}","messagePattern":"can't find class resource file (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/route/function/AutoPartitionByLong.java","lineNumber":104,"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":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/function/AutoPartitionByLong.java#L86-L122","documentation":"AutoPartitionByLong.initialize loads the rule's mapFile (long-range to node mapping) from the classpath via ClassLoader.getResourceAsStream. When the resource is missing, getResourceAsStream returns null and a RuntimeException 'can't find class resource file <mapFile>' is thrown, aborting rule initialization.","triggerScenarios":"init -> initialize with a mapFile name (from partition-long rule in rule.xml) that does not exist on the classpath, or the file is outside the classpath root so the relative resource lookup fails.","commonSituations":"Typo in mapFile (e.g. 'autopartion-long.txt' vs 'autopartition-long.txt'); file not packaged into the jar/classes directory; using an absolute filesystem path where a classpath resource is required after moving to a different deployment layout.","solutions":["Place the map file under src/main/resources (or MYCAT_HOME conf on the classpath) so it is on the classpath, and reference it by classpath-relative name.","Correct the mapFile value in rule.xml to match the actual resource filename exactly (case-sensitive).","Rebuild/redeploy so the file is packaged; verify with unzip -l or by checking conf directory.","If an external file is required, pre-load it via classpath or upgrade mycat versions that support file-system paths."],"exampleFix":"// before (rule.xml)\n<property name=\"mapFile\">/etc/mycat/partition.txt</property>\n// after\n<property name=\"mapFile\">partition-long.txt</property> <!-- file placed in conf/ on classpath -->","handlingStrategy":"validation","validationCode":"String mapFile = cfg.get(\"mapFile\");\nif (AutoPartitionByLong.class.getClassLoader().getResource(mapFile) == null) {\n    throw new IllegalStateException(\"mapFile missing from classpath: \" + mapFile);\n}","typeGuard":null,"tryCatchPattern":"try {\n    rule.init(config);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"can't find class resource file\")) {\n        LOGGER.error(\"place {} in conf/ (classpath) and restart\", cfg.get(\"mapFile\"));\n    } else { throw e; }\n}","preventionTips":["Keep rule map files in MYCAT_HOME conf and reference by classpath-relative name","Verify resource presence in CI before deploying config changes","Never use absolute filesystem paths for mapFile"],"tags":["classpath","resource","config","sharding-rule"],"backgroundTag":"file-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"}