MyCATApache/Mycat-Server · critical · RuntimeException

can't find class resource file

Error message

can't find class resource file {mapFile}

What it means

PartitionByFileMap.initialize() loads its key->partition mapping from a classpath resource named by the mapFile property. When getResourceAsStream returns null (resource not on the classpath), it throws this RuntimeException. The shard function cannot be initialized without its mapping file.

Solutions

  1. Place the map file in the Mycat classpath (conf directory) and ensure mapFile matches its exact name.
  2. Verify with the same classloader path: the file must be resolvable via getResourceAsStream, not an absolute filesystem path.
  3. Check filename case and extension; Linux classpath lookup is case-sensitive.

Example fix

// before
<property name="mapFile">partition-map.txt</property> <!-- file missing from conf/ -->
// after
cp partition-map.txt /opt/mycat/conf/
<property name="mapFile">partition-map.txt</property>
Defensive patterns

Strategy: validation

Validate before calling

boolean found = PartitionByFileMap.class.getClassLoader().getResource(mapFile) != null;
if (!found) throw new IllegalStateException("mapFile not on classpath: " + mapFile);

Try / catch

try {
    function.init();
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("can't find class resource file")) {
        // fail deployment fast with pointer to missing conf file
    }
}

Prevention

When it happens

Trigger: Calling init() with a mapFile that does not exist, a relative path not under the classpath, a wrong filename/case, or the file not being packaged/deployed to the Mycat conf classpath directory.

Common situations: Deploying rule.xml to a new server without copying the map file; typo in mapFile name; file placed in filesystem path instead of classpath; packaging (war/jar) excludes the resource.

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/6f13bca6f26dc13d. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/route/function/PartitionByFileMap.java:116

			throw new IllegalArgumentException(new StringBuilder().append("columnValue:").append(columnValue).append(" Please check if the format satisfied.").toString(),e);
		}
	}
	
	@Override
	public int getPartitionNum() {
		Set<Integer> set = new HashSet<Integer>(app2Partition.values());
		int count = set.size();
		return count;
	}

	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));
			
			app2Partition = new HashMap<Object, Integer>();
			
			for (String line = null; (line = in.readLine()) != null;) {
				line = line.trim();
				if (line.startsWith("#") || line.startsWith("//")) {
					continue;
				}
				int ind = line.indexOf('=');
				if (ind < 0) {
					continue;
				}
				try {
					String key = line.substring(0, ind).trim();
					int pid = Integer.parseInt(line.substring(ind + 1).trim());

View on GitHub (pinned to 65f8d8beb7)