Fix 'Invalid class name' error in Linux shell scripts
This error pops up when a shell script tries to use a Java class name with bad formatting. Here's how to spot it and fix it fast.
You're running a shell script that calls Java — maybe a cron job that processes CSV files, or a build script for a small business web app. Suddenly you see: Error: Invalid class name. Script dies. You check the class name, it looks right. But Java hates it.
I've seen this a dozen times. Last month a client's automated billing script failed every Monday at 2 AM because of this exact error. The class name was "com.company.InvoiceGenerator" — looked fine, but the script had a hidden trailing space after the class name. Drove them nuts for three weeks before they called me.
Root cause
Java's strict about class naming. The Invalid class name error means the string you're passing to java doesn't match the rules:
- It must be the fully qualified class name (e.g.,
com.example.MyClass) - No file extensions like
.classor.java - No path separators like slashes or backslashes
- No extra whitespace, newlines, or invisible characters
- The class must be in the classpath
Most common triggers: typos, extra spaces, using a file path instead of a class name, or the class not being compiled into a .class file in the classpath.
Fix it in 4 steps
- Check the exact string being passed to Java. In your shell script, add
echo "DEBUG: Class name is '$CLASSNAME'"right before the Java call. The single quotes will show any hidden characters. If you see a space before the closing quote, that's your problem. - Remove file extension and path. If your script has
java MyClass.class, change it tojava MyClass. If it hasjava /path/to/MyClass, change it tojava -cp /path/to MyClass. - Set classpath properly. Use the
-cpor-classpathflag to point to the directory or JAR containing the class. Example:java -cp /opt/myapp/bin com.example.MyClass. Don't rely on CLASSPATH environment variable — it's ignored byjavain some versions unless you set it explicitly. - Verify the class exists. Run
jar tf /path/to/jar.jar | grep MyClassfor JARs, orfind /path/to/classes -name '*.class' | grep MyClassfor directory-based classpaths. If the class isn't there, recompile your Java code.
If it still fails
Check these edge cases:
- Trailing newline in variable. If you read the class name from a file or variable, use
CLASSNAME=$(echo "$CLASSNAME" | tr -d '\n\r')to strip newlines. - Java version mismatch. Some older JVMs reject class names that are too long (over 256 chars). Check with
java -version. - Hidden Unicode characters. If you copy-pasted the class name from a web page or PDF, it might have non-ASCII characters. Retype it manually in a plain text editor.
- Script encoding. Make sure your script is saved as UTF-8 without BOM. BOM characters can sneak into the class name string.
Still stuck? Strip it down to a one-liner: java -cp . HelloWorld using a simple test class. If that works, your script is mangling the string. If it doesn't, Java's broken — reinstall it.
Was this solution helpful?