The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
As you saw in the previous step, the Java runtime does not automatically install a Security Manager when it runs an application. To apply the same security policy to an application found on the local file system as to downloaded sandbox applets, you can invoke the interpreter with the new -Djava.security.manager
command line argument.
To execute the GetProps
application with the default security manager, type the following:
java -Djava.security.manager GetProps
Here's the output from the program:
C:\TEST>java -Djava.security.manager GetProps About to get os.name property value The name of your operating system is: SunOS About to get java.version property value The version of the JVM you are running is: 1.7.0 About to get user.home property value Caught exception java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.home" "read")
The process is shown in the following figure.
The Java runtime loads a default policy file by default and grants all code permission to access some commonly useful properties such as "os.name"
and "java.version"
. These properties are not security-sensitive, so granting these permissions does not normally pose a security risk.
The other properties GetProps
tries to access, "user.home"
and "java.home"
, are not among the properties for which the system policy file grants read permission. Thus as soon as GetProps
attempts to access the first of these properties ("user.home"
), the security manager prevents the access and reports an AccessControlException
. This exception indicates that the policy currently in effect, which consists of entries in one or more policy files, doesn't allow permission to read the "user.home"
property.
The default policy file, java.policy
is (by default) located at:
java.home\lib\security\java.policy
java.home/lib/security/java.policy
Note that java.home represents the value of the "java.home"
property, which is a system property specifying the directory into which the JRE was installed. Thus if the JRE was installed in the directory named C:\jdk\jre
on Windows and /jdk/jre
on UNIX, the system policy file is located at:
C:\jdk\jre\lib\security\java.policy
/jdk/jre/lib/security/java.policy