DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Trending

  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • A Modern Stack for Building Scalable Systems
  • Agile and Quality Engineering: A Holistic Perspective
  • AI’s Role in Everyday Development
  1. DZone
  2. Coding
  3. Java
  4. Using sun.misc.Unsafe in Java 9

Using sun.misc.Unsafe in Java 9

This quick guide will take you through Unsafe in Java 9, saved from the trash heap by a public campaign, all while using the recent Java 9 EA release.

By 
Greg Luck user avatar
Greg Luck
·
Mar. 06, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
28.7K Views

Join the DZone community and get the full member experience.

Join For Free

The Java 9 EA version is out and we can now see how to use sun.misc.Unsafe. I led the public campaign to retain access to it in Java 9, which was ultimately successful, leading to the amendments to JEP 260.

So, how did things end up?

Getting Set Up

First, you need to download Java 9 EA. For an IDE, I use IntelliJ IDEA. You need the new 2017.1 Public Preview, which came out 27 February 2017. Earlier versions don’t work with Java 9.

The JDK.Unsupported Module

Sun.misc.Unsafe is now available in the jdk.unsupported module. This module is present in the full JRE and JDK images.

Here is the module declaration for jdk.unsupported:

module jdk.unsupported {
    exports sun.misc;
    exports sun.reflect;
    exports com.sun.nio.file;

    opens sun.misc;
    opens sun.reflect;
}


As you can see, sun.misc is exported.

Using It

I have a sample project with a package java9unsafe and a module with the same name.

To use Unsafe, you need to add jdk.unsupported to your code’s module declaration:

module java9unsafe {
    requires jdk.unsupported;
}


Fortunately, IDEA will detect the declaration if missing and suggest adding it for you when you hover over your import statement.

Then you can use Unsafe. Note you have to indirectly get at the Unsafe instance via reflection otherwise you get a security exception.

module jdk.unsupported {

public class Java9Unsafe {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        System.out.println("The address size is: " + getUnsafe().addressSize());
    }

    @SuppressWarnings("restriction")
    private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
        Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
        singleoneInstanceField.setAccessible(true);
        return (Unsafe) singleoneInstanceField.get(null);
    }

}


And the answer: The address size is: 8.

Java (programming language)

Published at DZone with permission of Greg Luck, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!