Java
Jarfile Error
Troubleshooting
Software Development
Error Resolution

What causes the "Unable to access jarfile" error in Java?

Codemia Knowledge Hub ·

The "Unable to access jarfile" error means the JVM cannot find or read the JAR file you passed to java -jar. The most common cause is a wrong file path, but missing permissions, filename typos, unquoted spaces in paths, or a corrupt JAR can also trigger it. It is always a file access problem, not a Java code problem.

How java -jar resolves the file

When you run java -jar myapp.jar, the JVM does not search PATH or CLASSPATH. It treats the argument as a file path relative to the current working directory (or as an absolute path if you give one). If the file is not at that exact location, or the process cannot read it, you get:

 
Error: Unable to access jarfile myapp.jar

The message comes from the JVM launcher before any Java code runs. It is not a Java exception; it is the operating system reporting that the file could not be opened.

Wrong file path or filename

This is the most frequent cause: the path in your command does not match where the file actually lives.

bash
1# You typed this
2java -jar /opt/app/myapp.jar
3
4# But the file is actually here
5ls /opt/app/
6# my-app.jar  (different name)

Verify the path and filename with ls or dir:

bash
1# Linux/macOS
2ls -la /opt/app/*.jar
3
4# Windows
5dir C:\opt\app\*.jar

Then use the exact path:

bash
java -jar /opt/app/my-app.jar

On Linux, MyApp.jar and myapp.jar are different files. Windows and macOS (with its default case-insensitive APFS volume) ignore case, so a script that works there can fail on Linux.

Spaces in the path

Unquoted paths with spaces get split into separate arguments:

bash
1# Broken: shell interprets "My" and "App/server.jar" as separate arguments
2java -jar /opt/My App/server.jar
3
4# Fixed: quote the path
5java -jar "/opt/My App/server.jar"

The same happens on Windows:

batch
1:: Broken
2java -jar C:\Program Files\MyApp\server.jar
3
4:: Fixed
5java -jar "C:\Program Files\MyApp\server.jar"

The JAR does not exist

The JAR was deleted, moved, or never built:

bash
# Check if the file exists
test -f /opt/app/myapp.jar && echo "exists" || echo "missing"

Typical reasons:

  • The build step (mvn package, gradle build) failed silently and never produced the JAR
  • A deployment script moved the file to a different directory
  • A cron job or CI pipeline cleaned the file up

Rebuild or redeploy, then confirm the file exists before running:

bash
mvn clean package -DskipTests
ls -la target/*.jar
java -jar target/myapp-1.0.0.jar

Insufficient read permissions

The user running java cannot read the JAR:

bash
1# Check permissions
2ls -la /opt/app/myapp.jar
3# -rw------- 1 root root 15M Jun 18 10:00 myapp.jar
4# Only root can read this file
5
6# Fix: grant read permission
7chmod +r /opt/app/myapp.jar
8
9# Or run as the correct user
10sudo -u appuser java -jar /opt/app/myapp.jar

On Windows, right-click the file, open Properties, then the Security tab, and confirm the executing user has Read permission.

Running from the wrong working directory

Relative paths resolve from the current working directory:

bash
1# Your JAR is in /opt/app/
2# But your terminal is in /home/user/
3pwd
4# /home/user
5
6java -jar myapp.jar
7# Error: Unable to access jarfile myapp.jar
8
9# Fix: use absolute path
10java -jar /opt/app/myapp.jar
11
12# Or change directory first
13cd /opt/app && java -jar myapp.jar

This is especially common in systemd services, cron jobs, and Docker containers, where the working directory may not be what you expect. Wrapper scripts that cd somewhere before running java -jar cause the same problem: relative paths resolve from the new directory, not from where you invoked the script.

A corrupt or invalid JAR

A truncated download, incomplete build, or wrong file type can also produce the error:

bash
1# Verify the file is actually a JAR (which is a ZIP format)
2file myapp.jar
3# myapp.jar: Java archive data (JAR)
4
5# If it says "ASCII text" or "HTML document", the download failed
6# and you got an error page instead of the actual file
7
8# Test archive integrity
9jar tf myapp.jar > /dev/null && echo "valid" || echo "corrupt"

If the JAR path is a symbolic link, its target might not exist:

bash
1ls -la /opt/app/myapp.jar
2# lrwxrwxrwx 1 user user 25 Jun 18 10:00 /opt/app/myapp.jar -> /opt/releases/v1.2.3.jar
3
4# Check if the target exists
5ls -la /opt/releases/v1.2.3.jar

Diagnostic checklist

CheckCommandWhat It Verifies
File existsls -la /path/to/app.jarPath and filename are correct
File is readabletest -r /path/to/app.jar && echo okCurrent user has read permission
File is a valid JARfile /path/to/app.jarNot a corrupt or wrong file type
Java is installedjava -versionJVM is available and on PATH
Working directorypwdRelative paths resolve correctly
Symlink target existsreadlink -f /path/to/app.jarLink is not broken
No path spaces issueWrap path in quotesShell does not split the argument

Platform-specific checks

Linux/macOS

bash
1# Find the JAR if you forgot where it is
2find / -name "myapp*.jar" -type f 2>/dev/null
3
4# Check SELinux context (RHEL/CentOS)
5ls -Z /opt/app/myapp.jar

Windows

batch
1:: Check JAVA_HOME
2echo %JAVA_HOME%
3
4:: Use forward slashes or escaped backslashes in scripts
5java -jar "C:/opt/app/myapp.jar"

Docker

dockerfile
1# Common mistake: COPY to wrong path
2COPY target/myapp.jar /app/myapp.jar
3WORKDIR /app
4
5# Verify during build
6RUN ls -la /app/myapp.jar
7
8CMD ["java", "-jar", "/app/myapp.jar"]

Use the exec form of CMD (JSON array) to avoid shell interpretation issues, and make sure COPY puts the file at the path you reference in CMD.

Less obvious causes

Using ~ in the path. Tilde expansion is a shell feature. Where no shell expands it (such as a systemd ExecStart line without shell wrapping), the JVM looks for a literal directory named ~.

Confusing -cp with -jar. -jar tells Java to run a JAR file; -cp adds to the classpath. To run a class from a JAR on the classpath, use -cp myapp.jar com.example.Main instead of -jar myapp.jar.

Antivirus quarantine. Security software can quarantine JAR files because they are executable archives. If the file appears to exist but the JVM still cannot access it, check your antivirus logs.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms