MyCATApache/Mycat-Server · error · RuntimeException

can't find class resource file

Error message

can't find class resource file ${mapFile}

What it means

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.

Solutions

  1. 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.
  2. Correct the mapFile value in rule.xml to match the actual resource filename exactly (case-sensitive).
  3. Rebuild/redeploy so the file is packaged; verify with unzip -l or by checking conf directory.
  4. If an external file is required, pre-load it via classpath or upgrade mycat versions that support file-system paths.

Example fix

// before (rule.xml)
<property name="mapFile">/etc/mycat/partition.txt</property>
// after
<property name="mapFile">partition-long.txt</property> <!-- file placed in conf/ on classpath -->
Defensive patterns

Strategy: validation

Validate before calling

String mapFile = cfg.get("mapFile");
if (AutoPartitionByLong.class.getClassLoader().getResource(mapFile) == null) {
    throw new IllegalStateException("mapFile missing from classpath: " + mapFile);
}

Try / catch

try {
    rule.init(config);
} catch (RuntimeException e) {
    if (e.getMessage().startsWith("can't find class resource file")) {
        LOGGER.error("place {} in conf/ (classpath) and restart", cfg.get("mapFile"));
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/4a3920f6d2e46de1. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/route/function/AutoPartitionByLong.java:104

		/*
		 * fix #1284 这里的统计应该统计Range的nodeIndex的distinct总数
		 */
		Set<Integer> distNodeIdxSet = new HashSet<Integer>();
		for(LongRange range : longRongs) {
			distNodeIdxSet.add(range.nodeIndx);
		}
		int nPartition = distNodeIdxSet.size();
		return nPartition;
	}

	private void initialize() {
		BufferedReader in = null;
		try {
			// FileInputStream fin = new FileInputStream(new File(fileMapPath));
			InputStream fin = this.getClass().getClassLoader()
					.getResourceAsStream(mapFile);
			if (fin == null) {
				throw new RuntimeException("can't find class resource file "
						+ mapFile);
			}
			in = new BufferedReader(new InputStreamReader(fin));
			LinkedList<LongRange> longRangeList = new LinkedList<LongRange>();

			for (String line = null; (line = in.readLine()) != null;) {
				line = line.trim();
				if (line.startsWith("#") || line.startsWith("//")) {
					continue;
				}
				int ind = line.indexOf('=');
				if (ind < 0) {
					System.out.println(" warn: bad line int " + mapFile + " :"
							+ line);
					continue;
				}
					String pairs[] = line.substring(0, ind).trim().split("-");
					long longStart = NumberParseUtil.parseLong(pairs[0].trim());

View on GitHub (pinned to 65f8d8beb7)