MyCATApache/Mycat-Server · critical · RuntimeException

can't find class resource file

Error message

can't find class resource file {mapFile}

What it means

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.

Solutions

  1. Copy the prefix pattern map file into the classpath (Mycat conf directory or src/main/resources) using the exact name referenced by mapFile.
  2. Check the mapFile property for typos, leading slashes, or absolute paths and make it a classpath-relative resource name.
  3. Rebuild the deployment artifact so the resource is packaged, then restart Mycat.
  4. As a code change, add a filesystem fallback (FileInputStream) when the classpath lookup fails.

Example fix

// before
<property name="mapFile">/home/user/prefix-partition.txt</property>
// after: file placed in conf/ (classpath)
<property name="mapFile">prefix-partition.txt</property>
Defensive patterns

Strategy: validation

Validate before calling

boolean ok = PartitionByPrefixPattern.class.getClassLoader().getResource(mapFile) != null;
if (!ok) throw new IllegalStateException("missing classpath resource for mapFile: " + mapFile);

Try / catch

try { rule.init(); } catch (RuntimeException e) { if (e.getMessage().startsWith("can't find class resource file")) { /* correct mapFile and redeploy */ } throw e; }

Prevention

When it happens

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

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

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/main/java/io/mycat/route/function/PartitionByPrefixPattern.java:110

		/*
		 * 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)