Access restriction The type 'Application' is not API restriction on required library rt.jar
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding Access Restriction: "The type 'Application' is not API (restriction on required library rt.jar)"
In the realm of Java development, compiling and running code requires understanding how libraries and APIs are managed. One particular issue that developers might encounter is the access restriction warning: “The type 'Application' is not API (restriction on required library rt.jar)”. This article aims to unravel this message and provide practical insights into how it arises, what it indicates, and how you may resolve it in a Java development environment.
What is rt.jar?
Before delving into the error, it’s essential to comprehend what rt.jar is.
rt.jar stands for "runtime JAR" and contains all of the runtime classes in Java's core API—essentially the packaged libraries that implement the Java SE platform. However, in Java 9 and later, the rt.jar has been replaced by modules under the Java Platform Module System (JPMS).
Context of the Error
The error message “The type 'Application' is not API…” typically surfaces when you are attempting to access types or APIs that are not intended for public use. These might be internal APIs or libraries meant solely for the functioning of the Java platform itself and not for direct access by Java applications.
Technical Background
Java provides a mechanism for the encapsulation of classes and interfaces through packages and modules. Some interfaces, like com.sun classes or some internal Java core classes, are considered non-public. These classes are not intended for general developers and are subject to change with different Java releases.
Example Scenario
Let's consider you have a class file that attempts to import and use Java internal classes. You might encounter a snippet like:
The above code's compilation might trigger an access restriction warning or error because Unsafe is an internal class not meant for public API consumption.
Key Points to Consider
| Aspect | Details & Explanation |
| Access Restriction | Certain classes are encapsulated and restricted from API access to ensure stability and security. |
| Java Version Differences | Java 9+ introduced modules, enhancing class organization. This affects access patterns significantly. |
| Use of Internal APIs | Direct usage can lead to compatibility issues with future releases. Use official APIs whenever possible. |
| Problematic Imports | Importing sun.* packages is inherently unstable. |
| Alternatives | Leverage public APIs provided by Java or third-party libraries. |
Causes of Access Restrictions
- Modularity in Java 9+:
- With the introduction of the module system, Java seeks to encapsulate internal APIs. By default, most internals are not exported, a choice aimed at reducing accidental dependencies on unstable internal classes.
- Security and Stability:
- Internal classes might rely on JVM specifics, and exposure could lead to security vulnerabilities or unintended behavior.
- Deprecation and Maintenance:
- Internal APIs might be deprecated faster than standard APIs, leading to maintenance challenges.
Resolving the Issue
Here are some strategies:
- Replace Internal API Usage:
- Seek equivalent functionality in the officially supported APIs or adopt third-party libraries that offer similar capabilities.
- Add Access Flags:
- For development purposes, it’s possible to bypass this with JVM arguments like
--add-exportsand--add-opens, but this should be approached cautiously.
- Adopt Jigsaw Features:
- Use the module-path and ensure dependencies are correctly designed within module boundaries.
- IDE Setup Adjustments:
- If you're using an IDE like Eclipse, adjust compiler settings to ignore or provide warnings instead of errors for access restrictions.
Conclusion
The "type 'Application' is not API" restriction echoes a fundamental principle in Java development: reliance on stable, public APIs ensures long-term software sustainability and security. With rigorous adoption of module systems post-Java 9, developers are encouraged to realign their codebases to avoid accessing internal Java types.
Adapting to these restrictions, while initially challenging for those used to rt.jar, fosters an ecosystem of clean, maintainable, and forward-compatible Java applications. Exercise diligence in understanding the tools provided by the Java ecosystem, and always prioritize using official APIs that adhere to stable contracts.
By meticulously navigating these restrictions, developers assure the robustness and resilience of their applications against the evolving Java landscape.

