copying the JRE directory will we lose class sharing?
The class data exchange file will be valid only if the JVM version and the path to the load class remain unchanged. Starting at 8u40, as a result of the JDK-8046070, the JVM will refuse to load the CDS archive, even if the JRE directory is renamed or moved.
Java 7 still allows you to reuse the CDS archive when copying the JRE directory, but this is not a reliable feature, and I would not recommend doing this.
can be solved by regenerating the shared archive (using java -Xshare: dump)
Yes, that is the right way. This will ensure the integrity of the created CDS archive and also preserve the size of the installation package.
Is there a mechanism in Java code to determine if data exchange between classes is active or not?
Yes, reading the UseSharedSpaces flag using JMX:
HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy( ManagementFactory.getPlatformMBeanServer(), "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class); System.out.println(bean.getVMOption("UseSharedSpaces"));
You can also enforce the CDS requirement with the -XX:+RequireSharedSpaces JVM flag.
"Is the Java code stored in the class data sharing archive (classes.jsa) compiled initially or is it bytecode?"
Depends on what you mean by "compiled initially." Class metadata is stored in a proprietary platform-specific format. But the bytecode does not compile. There is no time-ahead compilation; Java bytecode is stored in the CDS archive as is.
source share