java虚拟机详解 免费
java 虚拟机规范

java 虚拟机规范Java虚拟机规范(Java Virtual Machine Specification)是由Java语言设计者为了保证Java程序在任何平台上都能够运行,而定义的虚拟机的行为规范。
本文将详细介绍Java虚拟机规范,内容约1000字。
Java虚拟机规范定义了Java虚拟机(JVM)的结构、指令集、运行时数据区域、内存管理、异常处理、线程和同步等方面的规范。
通过遵循这些规范,开发者可以编写出具有良好可移植性和跨平台性的Java程序。
首先,Java虚拟机规范定义了JVM的结构。
JVM由类加载器、运行时数据区域、执行引擎和本地方法接口等组成。
类加载器负责加载类文件,并进行验证、解析和初始化。
运行时数据区域则包括了方法区、堆、虚拟机栈、本地方法栈和程序计数器等。
执行引擎根据操作码执行相应的指令,并对操作数栈和局部变量表进行操作。
本地方法接口则是Java虚拟机与本地操作系统的接口。
其次,Java虚拟机规范定义了JVM的指令集。
指令集包括了加载、存储、算术运算、类型转换、比较和控制流等各种指令。
每个指令都对应了Java虚拟机的一种行为,可以通过指定操作数来执行相应的操作。
Java虚拟机规范还定义了JVM的运行时数据区域。
运行时数据区域包括了方法区、堆、虚拟机栈、本地方法栈和程序计数器。
方法区用于存储类的结构信息、常量池、静态变量和方法等。
堆用于存储对象实例和数组。
虚拟机栈用于存储方法调用的局部变量表、操作数栈、动态链接和方法返回值等。
本地方法栈用于支持本地方法的调用。
程序计数器则用于记录当前线程执行的字节码指令地址。
此外,Java虚拟机规范还定义了JVM的内存管理、异常处理、线程和同步等方面的规范。
内存管理包括了垃圾收集和内存分配等。
异常处理定义了异常的分类、异常的处理流程和异常的处理方式。
线程规范包括了线程的创建、启动、执行和销毁等。
同步规范则定义了同步的机制和语义。
总结起来,Java虚拟机规范定义了Java虚拟机的行为规范,包括了JVM的结构、指令集、运行时数据区域、内存管理、异常处理、线程和同步等方面的规范。
Java Card平台虚拟机规范(经典版)v3.2说明书

Java Card™Platform Virtual Machine Specification, Classic EditionVersion 3.2January 2023Java Card Platform Virtual Machine Specification, Classic Edition Version 3.2Copyright © 1998, 2023, Oracle and/or its affiliates. All rights reserved.The Specification provided herein is provided to you only under the Oracle Technology Network Developer License included herein as Annex A - Oracle Technology Network Developer License Terms.License Restrictions Warranty/Consequential Damages DisclaimerThis software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.Warranty DisclaimerThe information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.Restricted Rights NoticeIf this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable:U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government.Hazardous Applications NoticeThis software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.Trademark NoticeOracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.Third-Party Content, Products, and Services DisclaimerThis software or hardware and documentation may provide access to or information about content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth in an applicable agreement between you and Oracle.Documentation AccessibilityFor information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at /pls/topic/lookup?ctx=acc&id=docacc.Access to Oracle SupportOracle customers that have purchased support have access to electronic support through My Oracle Support. For information, visit /pls/topic/lookup?ctx=acc&id=info or visit/pls/topic/lookup?ctx=acc&id=trs if you are hearing impaired.ContentsPreface (19)Who Should Use This Specification (19)Before You Read This Specification (19)Shell Prompts (19)Typographic Conventions (20)Related Documentation (20)Third-Party Web Sites (20)Documentation Accessibility (21)Access to Oracle Support (21)Oracle Welcomes Your Comments (21)1Introduction (22)1.1 Motivation (22)1.2 The Java Card Virtual Machine (23)1.3 Java Language Security (25)1.4 Java Card Runtime Environment Security (25)2 A Subset of the Java Virtual Machine (27)2.1 Why a Subset is Needed (27)2.2 Java Card Platform Language Subset (27)2.2.1 Unsupported Items (27)2.2.1.1 Unsupported Features (27)2.2.1.1.1 Dynamic Class Loading (27)2.2.1.1.2 Security Manager (28)2.2.1.1.3 Finalization (28)2.2.1.1.4 Threads (28)2.2.1.1.5 Cloning (28)2.2.1.1.6 Access Control in Java Packages (28)2.2.1.1.7 Typesafe Enums (28)2.2.1.1.8 Enhanced for Loop (29)2.2.1.1.10 Runtime Visible Metadata (Annotations) (29)2.2.1.1.11 Assertions (29)2.2.1.2 Unsupported Keywords (29)2.2.1.3 Unsupported Types (29)2.2.1.4 Unsupported Classes (29)2.2.1.4.1 System (30)2.2.2 Supported Items (30)2.2.2.1 Supported Features (30)2.2.2.1.1 Packages (30)2.2.2.1.2 Dynamic Object Creation (30)2.2.2.1.3 Virtual Methods (30)2.2.2.1.4 Interfaces (30)2.2.2.1.5 Exceptions (30)2.2.2.1.6 Generics (31)2.2.2.1.7 Static Import (31)2.2.2.1.8 Runtime Invisible Metadata (Annotations) (31)2.2.2.2 Supported Keywords (31)2.2.2.3 Supported Types (32)2.2.2.4 Supported Classes (32)2.2.2.4.1 Object (32)2.2.2.4.2 Throwable (32)2.2.3 Optionally Supported Items (32)2.2.3.1 Integer Data Type (33)2.2.3.2 Object Deletion Mechanism (33)2.2.4 Limitations of the Java Card Virtual Machine (33)2.2.4.1 Limitations of Packages (33)2.2.4.1.1 Packages in a Java Card CAP file (33)2.2.4.1.2 Package References (33)2.2.4.1.3 Package Name (33)2.2.4.2 Limitations of Classes (34)2.2.4.2.1 Classes in a Package (34)2.2.4.2.3 Static Fields (34)2.2.4.2.4 Static Methods (34)2.2.4.3 Limitations of Objects (34)2.2.4.3.1 Methods (34)2.2.4.3.2 Class Instances (34)2.2.4.3.3 Arrays (34)2.2.4.4 Limitations of Methods (34)2.2.4.5 Limitations of Switch Statements (35)2.2.4.6 Limitations of Class Initialization (35)2.2.5 Multiselectable Applets Restrictions (35)2.2.6 Java Card Platform Remote Method Invocation (RMI) Restrictions (35)2.2.6.1 Remote Classes and Remote Interfaces (36)2.2.6.2 Access Control of Remote Interfaces (36)2.2.6.3 Parameters and Return Values (36)2.3 Java Card VM Subset (36)2.3.1 Class File Subset (37)2.3.1.1 Not Supported in Class Files (37)2.3.1.1.1 Class Access Flags (37)2.3.1.1.2 Field Descriptors (37)2.3.1.1.3 Constant Pool (37)2.3.1.1.4 Fields (37)2.3.1.1.5 Methods (37)2.3.1.2 Supported in Class Files (37)2.3.1.2.1 ClassFile (37)2.3.1.2.2 Field Descriptors (37)2.3.1.2.3 Method Descriptors (38)2.3.1.2.4 Constant Pool (38)2.3.1.2.5 Fields (38)2.3.1.2.6 Methods (38)2.3.1.2.7 Attributes (38)2.3.2 Bytecode Subset (38)2.3.2.2 Supported Bytecodes (40)2.3.2.3 Static Restrictions on Bytecodes (42)2.3.2.3.1 ldc, ldc_w (42)2.3.2.3.2 lookupswitch (42)2.3.2.3.3 tableswitch (42)2.3.2.3.4 wide (42)2.3.3 Exceptions (43)2.3.3.1 Uncaught and Uncatchable Exceptions (43)2.3.3.2 Checked Exceptions (43)2.3.3.3 Runtime Exceptions (44)2.3.3.4 Errors (44)3Structure of the Java Card Virtual Machine (46)3.1 Data Types and Values (46)3.2 Words (46)3.3 Runtime Data Areas (47)3.4 Contexts (47)3.5 Frames (47)3.6 Representation of Objects (48)3.7 Special Initialization Methods (48)3.8 Exceptions (48)3.9 Binary File Formats (48)3.10 Instruction Set Summary (48)3.10.1 Types and the Java Card Virtual Machine (49)4Binary Representation (51)4.1 Java Card Platform File Formats (51)4.1.1 Export File Format (51)4.1.2 CAP File Format (52)4.1.3 JAR File Container (52)4.2 AID-based Naming (53)4.2.1 The AID Format (53)4.2.2 AID Usage (53)4.2.2.2 Applet AID namespace (53)4.2.2.3 Package AID namespace (54)4.2.2.3 Custom Component AID namespace (54)4.3 Token-based Linking (54)4.3.1 Externally Visible Items (54)4.3.2 Private Tokens (55)4.3.3 The Export File and Conversion (55)4.3.4 References – External and Internal (55)4.3.5 Installation and Linking (56)4.3.6 Token Assignment (56)4.3.7 Token Details (56)4.3.7.1 Package (56)4.3.7.2 Classes and Interfaces (56)4.3.7.3 Static Fields (57)4.3.7.4 Static Methods and Constructors (57)4.3.7.5 Instance Fields (57)4.3.7.6 Virtual Methods (58)4.3.7.7 Interface Methods (58)4.4 Binary Compatibility (59)4.5 CAP and Package Versions (60)4.5.1 Assigning (60)4.5.2 Linking (60)5The Export File Format (62)5.1 Export File Name (62)5.2 Containment in a JAR File (62)5.3 Ownership (62)5.4 Hierarchies Represented (63)5.5 Export File (63)5.6 Constant Pool (65)5.6.1 CONSTANT_Package (65)5.6.2 CONSTANT_Classref (67)5.6.4 CONSTANT_Utf8 (68)5.7 Classes and Interfaces (68)5.8 Fields (71)5.9 Methods (73)5.10 Attributes (75)5.10.1 ConstantValue Attribute (75)6The CAP File Format (77)6.1 CAP File Overview (77)6.2 Component Model (78)6.2.1 Containment in a JAR File (79)6.2.2 Defining New Components (80)6.3 Installation (81)6.4 Header Component (81)6.5 Directory Component (85)6.6 Applet Component (90)6.7 Import Component (92)6.8 Constant Pool Component (93)6.8.1 CONSTANT_Classref (95)6.8.2 CONSTANT_InstanceFieldref, CONSTANT_VirtualMethodref, CONSTANT_SuperMethodref (96)6.8.3 CONSTANT_StaticFieldref and CONSTANT_StaticMethodref (98)6.9 Class Component (100)6.9.1 type_descriptor (102)6.9.2 interface_info, class_info_compact and class_info_extended (104)6.9.2.1 interface_info, class_info_compact and class_info_extended Shared Items (105)6.9.2.2 interface_info Items (106)6.9.2.3 class_info_compact and class_info_extended Items (107)6.9.2.4 method_block_info (111)6.9.2.5 implemented_interface_info (112)6.9.2.6 remote_interface_info (113)6.9.2.7 public_virtual_method_token_mapping (115)6.10 Method Component (116)6.10.1 method_component_block (117)6.10.2 Exception Handler Example (118)6.10.3 exception_handler_info (119)6.10.4 method_info (121)6.11 Static Field Component (123)6.12 Reference Location Component (127)6.12.1 reference_location_component_block (128)6.13 Export Component (130)6.14 Descriptor Component (133)6.14.1 package_descriptor_info (135)6.14.2 class_descriptor_info_compact and class_descriptor_info_extended (135)6.14.3 field_descriptor_info (137)6.14.4 method_descriptor_info_compact and method_descriptor_info_extended (139)6.14.5 type_descriptor_info (142)6.15 Debug Component (143)6.15.1 package_debug_info_compact and package_debug_info_extended Structures (145)6.15.2 The class_debug_info_compact and class_debug_info_extended Structures (145)6.15.2.1 The field_debug_info Structure (148)6.15.2.2 The method_debug_info_compact and method_debug_info_extended Structures (150)6.16 Static Resource Component (154)7Java Card Virtual Machine Instruction Set (157)7.1 Assumptions: The Meaning of “Must” (157)7.2 Reserved Opcodes (157)7.3 Virtual Machine Errors (157)7.4 Security Exceptions (158)7.5 The Java Card Virtual Machine Instruction Set (159)7.5.1 aaload (160)7.5.2 aastore (161)7.5.3 aconst_null (163)7.5.4 aload (163)7.5.5 aload_<n> (164)7.5.6 anewarray (165)7.5.8 arraylength (166)7.5.9 astore (166)7.5.10 astore_<n> (167)7.5.11 athrow (168)7.5.12 baload (169)7.5.13 bastore (169)7.5.14 bipush (170)7.5.15 bspush (171)7.5.16 checkcast (171)7.5.17 dup (173)7.5.18 dup_x (174)7.5.19 dup2 (175)7.5.20 getfield_<t> (175)7.5.21 getfield_<t>_this (176)7.5.22 getfield_<t>_w (178)7.5.23 getstatic_<t> (179)7.5.24 goto (180)7.5.25 goto_w (181)7.5.26 i2b (181)7.5.27 i2s (182)7.5.28 iadd (182)7.5.29 iaload (183)7.5.30 iand (184)7.5.31 iastore (184)7.5.32 icmp (185)7.5.33 iconst_<i> (186)7.5.34 idiv (186)7.5.35 if_acmp<cond> (187)7.5.36 if_acmp<cond>_w (188)7.5.37 if_scmp<cond> (189)7.5.38 if_scmp<cond>_w (189)7.5.40 if<cond>_w (191)7.5.41 ifnonnull (192)7.5.42 ifnonnull_w (193)7.5.43 ifnull (193)7.5.44 ifnull_w (194)7.5.45 iinc (194)7.5.46 iinc_w (195)7.5.47 iipush (195)7.5.48 iload (196)7.5.49 iload_<n> (197)7.5.50 ilookupswitch (197)7.5.51 imul (198)7.5.52 ineg (199)7.5.53 instanceof (200)7.5.54 invokeinterface (202)7.5.54.1 Interface Method Resolution (203)7.5.55 invokespecial (204)7.5.56 invokestatic (205)7.5.56.1 Super Method Resolution (206)7.5.57 invokevirtual (206)7.5.57.1 Virtual Method Resolution (207)7.5.58 ior (208)7.5.59 irem (208)7.5.60 ireturn (209)7.5.61 ishl (210)7.5.62 ishr (210)7.5.63 istore (211)7.5.64 istore_<n> (211)7.5.65 isub (212)7.5.66 itableswitch (213)7.5.67 iushr (214)7.5.69 jsr (215)7.5.70 new (216)7.5.71 newarray (216)7.5.72 nop (217)7.5.73 pop (218)7.5.74 pop2 (218)7.5.75 putfield_<t> (219)7.5.76 putfield_<t>_this (220)7.5.77 putfield_<t>_w (222)7.5.78 putstatic_<t> (223)7.5.79 ret (224)7.5.80 return (225)7.5.81 s2b (225)7.5.82 s2i (226)7.5.83 sadd (226)7.5.84 saload (227)7.5.85 sand (228)7.5.86 sastore (228)7.5.87 sconst_<s> (229)7.5.88 sdiv (229)7.5.89 sinc (230)7.5.90 sinc_w (231)7.5.91 sipush (231)7.5.92 sload (232)7.5.93 sload_<n> (232)7.5.94 slookupswitch (233)7.5.95 smul (234)7.5.96 sneg (234)7.5.97 sor (235)7.5.98 srem (235)7.5.99 sreturn (236)7.5.101 sshr (237)7.5.102 sspush (238)7.5.103 sstore (238)7.5.104 sstore_<n> (239)7.5.105 ssub (239)7.5.106 stableswitch (240)7.5.107 sushr (241)7.5.108 swap_x (241)7.5.109 sxor (242)8Tables of Instructions (244)8.1 Instructions by Opcode Value (244)8.2 Instructions by Opcode Mnemonic (248)Glossary (254)Annex A - Oracle Technology Network Developer License Terms (269)FiguresFigure 1-1: Java Card Application or Library Conversion (23)Figure 1-2: Java Card Application or Library Installation (24)Figure 4-1: Mapping Package Identifiers to AIDs (54)Figure 4-2: Binary Compatibility Example (59)TablesTable 2-1: Unsupported Java Constant Pool Tags (37)Table 2-2: Supported Java Constant Pool Tags (38)Table 2-3: Support of Java Checked Exceptions (43)Table 2-4: Support of Java Runtime Exceptions (44)Table 2-5: Support of Java Errors (44)Table 3-1: Type Support in the Java Card Virtual Machine Instruction Set (49)Table 3-2: Storage Types and Computational Types (50)Table 4-1: AID Format (53)Table 4-2: Token Range, Type and Scope (56)Table 4-3: Tokens For Instance Fields (57)Table 5-1: Export File Constant Pool Tags (65)Table 5-2: Export File Package Flags (66)Table 5-3: Export File Class Access and Modifier Flags (69)Table 5-4: Export File Field Access and Modifier Flags (72)Table 5-5: Export File Method Access and Modifier Flags (74)Table 6-1: CAP File Component Tags (79)Table 6-2: CAP File Component File Names (79)Table 6-3: CAP File Flags (83)Table 6-4: CAP File Constant Pool Tags (94)Table 6-5: Type Descriptor Values (102)Table 6-6: Encoded Reference Type p1.c1 (103)Table 6-7: Encoded Byte Array Type (103)Table 6-8: Encoded Reference Array Type p1.c1 (103)Table 6-9: Encoded Method Signature ()V (104)Table 6-10: Encoded Method Signature (Lp1.ci;)S (104)Table 6-11: CAP File Interface and Class Flags (106)Table 6-12: CAP File Method Flags (122)Table 6-13: Segments of a Static Field Image (124)Table 6-14: Static Field Sizes (124)Table 6-15: Array Types (126)Table 6-16: One-byte Reference Location Example (129)Table 6-17: CAP File Class Descriptor Flags (136)Table 6-18: CAP File Field Descriptor Flags (138)Table 6-19: Primitive Type Descriptor Values (139)Table 6-20: CAP File Method Descriptor Flags (140)Table 6-21: Class Access and Modifier Flags (146)Table 6-22: Field Access and Modifier Flags (149)Table 6-23: Method Modifier Flags (151)Table 7-1: Example Instruction Entry (159)Table 7-2: Array Values (172)Table 7-3: Array Values (200)Table 7-4: Array Values (217)Table 8-1: Instructions by Opcode Value (244)Table 8-2: Instructions by Opcode Mnemonic (248)PrefaceJava Card technology combines a subset of the Java programming language with a runtime environment optimized for secure elements, such as smart cards and other tamper-resistant security chips. Java Card technology offers a secure and interoperable execution platform that can store and update multiple applications on a single resource-constrained device, while retaining the highest certification levels and compatibility with standards. Java Card developers can build, test, and deploy applications and services rapidly and securely. This accelerated process reduces development costs, increases product differentiation, and enhances value to customers.The Classic Edition of the Java Card Platform is defined by three specifications:∙Virtual Machine Specification, Java Card Platform, Version 3.2, Classic Edition,∙Runtime Environment Specification, Java Card Platform, Version 3.2, Classic Edition,∙Application Programming Interface, Java Card Platform, Version 3.2, Classic Edition.This document is a specification of the Classic Edition of the Java Card Platform, Version 3.2, Virtual Machine (Java Card VM).In this book, Java Card Platform refers to version 3.2 to distinguish it from all earlier versions. A vendor of a Java Card technology-enabled device provides an implementation of the Java Card RE. An implementation within the context of this specification refers to a vendor's implementation of the Java Card Virtual Machine (or Java Card VM), the Java Card Application Programming Interface (API), or other component, based on the Java Card technology specifications. A "reference implementation" is an implementation produced by Oracle. Application software written for the Java Card platform is referred to as a Java Card technology-based applet (Java Card applet or card applet).Who Should Use This SpecificationThis specification is intended to assist implementers of the Java Card RE in creating an implementation, developing a specification to extend the Java Card technology specifications, or in creating an extension to the runtime environment for the Java Card platform. This specification is also intended for Java Card applet developers who want a greater understanding of the Java Card technology specifications.Before You Read This SpecificationBefore reading this guide, you should be familiar with the Java programming language, the other Java Card technology specifications, and smart card technology. A good resource for becoming familiar with Java technology and Java Card technology located at:/technetwork/java/javacard/overview/Shell PromptsShell PromptC shell machine-name%C shell superuser machine-name#Bourne shell and Korn shell $Bourne shell and Korn shell superuser #Typographic ConventionsThe settings on your browser might differ from these settings. Typeface Meaning ExamplesAaBbCc123 The names of commands,files, and directories; on-screen computer output Edit your .login file. Use ls -a to list all files. % You have mail.AaBbCc123 What you type, whencontrasted with on-screen computer output %su Password:AaBbCc123 Book titles, new words orterms, words to beemphasized. Replacecommand-line variableswith real names orvalues. Read Chapter 6 in the User's Guide. These are called class options. You must be superuser to do this. To delete a file, type rm filename.Related DocumentationReferences to various documents or products are made in this guide, so you might want to have them available:∙Application Programming Interface, Java Card Platform, Version 3.2, Classic Edition∙Runtime Environment Specification, Java Card Platform, Version 3.2, Classic Edition∙The Java Language Specification (https:///javase/specs/)∙ISO 7816 Specification Parts 1-6. (https://)Third-Party Web SitesOracle is not responsible for the availability of third-party web sites mentioned in this document. Oracle does not endorse and is not responsible or liable for any content, advertising, products, or other materials that are available on or through such sites or resources. Oracle will not be responsible or liable for any actual or alleged damage or loss caused by or in connection with the use of or reliance on any such content, goods, or services that are available on or through such sites or resources.Documentation AccessibilityFor information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at:/pls/topic/lookup?ctx=acc&id=docacc.Access to Oracle SupportOracle customers that have purchased support have access to electronic support through My Oracle Support. For information, visit:/pls/topic/lookup?ctx=acc&id=infoOr, if you are hearing impaired, visit:/pls/topic/lookup?ctx=acc&id=trsOracle Welcomes Your CommentsOracle is interested in improving its documentation and welcomes your comments and suggestions.Please include the title of your document with your feedback:Virtual Machine Specification, Java Card Platform, v3.2, Classic Edition1IntroductionThis document specifies the Java Card Virtual Machine features required by the Classic Edition ofJava Card technology.∙It defines the subset of the Java Virtual Machine used for the Java Card Virtual Machine and list the supported and unsupported features.∙It defines the binary representation of the application code, the role and structure of the Export and CAP file formats and their use in the verification and linking process.∙It specifies the Java Card Virtual Machine byte-code set and its detailed behavior.1.1 MotivationJava Card technology enables programs written in the Java programming language to be run on secure elements such as smart cards and other tamper-resistant security chips. Developers can build and test programs using standard software development tools and environments, then convert them into a form that can be installed onto a Java Card technology-enabled device. Application software for the Java Card platform is called an applet, or more specifically, a Java Card applet (to distinguish it from browser applets).While Java Card technology enables programs written in the Java programming language to run on small devices such as smart cards, those are far too under-powered to support the full functionality of the Java platform. Therefore, the Java Card platform supports only a carefully chosen, customized subset of the features of the Java platform. This subset provides features that are well-suited for writing programs for small devices and preserves the object-oriented capabilities of the Java programming language.A simple approach to specifying a Java Card virtual machine would be to describe the subset of the features of the Java virtual machine that must be supported to allow for portability of source code across all Java Card technology enabled devices. Combining that subset specification and the information in Java Virtual Machine Specification, smart card and secure elements manufacturers could construct their own Java Card technology-based implementations (“Java Card implementations”). While that approach is feasible, it has a serious drawback. The resultant platform would be missing the important feature of binary portability of Java Card applets.The standards that define the Java platform allow for binary portability of Java programs across all Java platform implementations. This “write once, run anywhere” quality of Java programs is perhaps the most significant feature of the platform. Part of the motivation for the creation of the Java Cardplatform was to bring just this kind of binary portability to the embedded security and smart card industry. In a world with billions of secure elements with varying processors and configurations, the costs of supporting multiple binary formats for software distribution could be overwhelming.This Virtual Machine Specification, Java Card Platform, v3.2, Classic Edition is the key to providing binary portability. One way of understanding what this specification does is to compare it to its counterpart in the Java platform. The Java virtual machine specification defines a Java virtual machine as an engine that loads Java class files and executes them with a particular set of semantics. The class file is a central piece of the Java architecture, and it is the standard for the binary compatibility of the Java platform. The Virtual Machine Specification, Java Card Platform, v3.2, Classic Edition also defines a file format that is the standard for binary compatibility for the Java Card platform: the CAP file format is the form in which software is deployed to be loaded onto devices which implement a Java Card virtual machine.1.2 The Java Card Virtual MachineThe role of the Java Card virtual machine is best understood in the context of the process for production and deployment of software for the Java Card platform. There are several components that make up a Java Card system, including the Java Card virtual machine, the Converter for the Java Card platform (“Java Card Converter”), a terminal installation tool, and an installation program that runs on the device, as shown in Figure 1-1 and Figure 1-2.Figure 1-1: Java Card Application or Library ConversionFigure 1-2: Java Card Application or Library InstallationDevelopment of a Java Card applet begins as with any other Java program: a developer writes one or more Java classes, and compiles the source code with a Java compiler, producing one or more class files. The applet is run, tested and debugged on a workstation using simulation tools to emulate the device environment. Then, when an applet is ready to be downloaded to a device, the class files comprising the applet are converted to a CAP (converted applet) file using a Java Card Converter.The Java Card Converter takes as input all of the class files in one or more Java packages which make up a Java Card CAP file. A Java package that contains one or more non-abstract subclasses, direct or indirect, of the javacard.framework.Applet class is referred to as an applet package. Otherwise the package is referred to as a library package. A Java Card CAP file may contain only applet packages, only library packages or a combination of applet and library packages. Additionally, both applet and library packages in a Java Card CAP file can be public or private.A private library package in a Java Card CAP file is not listed in the Export Component (6.13 Export Component) of the CAP file and is therefore not visible outside the Java Card CAP file. Similarly, a private applet package in a Java Card CAP file is not listed in the Export Component (6.13 Export Component) of the CAP file, however, non-abstract direct or indirect subclasses of thejavacard.framework.Applet class are listed in the Applet Component (6.6 Applet Component) of the CAP file. For a public applet package in a Java Card CAP file, only public interfaces extending javacard.framework.Shareable are listed in the Export Component (6.13 Export Component) of the CAP file and are therefore visible outside the Java Card CAP file. For further details see The CAP File Format.The Java Card Converter also takes as input one or more export files. An export file contains name and link information for the contents of other packages that are imported by the classes being converted. The converter can also produce export files for the public applet and library packages in a CAP file.After conversion, the CAP file is copied to a terminal, such as a desktop computer with a card reader peripheral. Then an installation tool on the terminal loads the CAP file and transmits it to the Java Card technology-enabled device. An installation program on the device receives the contents of the CAP file。
java虚拟机的工作原理

java虚拟机的工作原理Java虚拟机(JVM)是Java程序运行的环境,它负责解释和执行Java字节码。
JVM的工作原理可以分为三个主要的部分:类加载、字节码执行和垃圾回收。
1.类加载:JVM通过类加载器将Java字节码加载到内存中。
类加载器根据类路径在文件系统或网络中查找并读取字节码文件,然后将其转化为JVM运行时数据结构,如类和方法的元数据。
加载完成后,JVM会在方法区中存储类的元数据,并在堆中分配内存来存储类的实例。
2.字节码执行:3.垃圾回收:JVM提供垃圾回收机制来自动释放不再使用的内存。
JVM会跟踪每个对象的引用,当一个对象没有引用时,即被视为垃圾。
垃圾回收器定期执行垃圾收集操作,释放垃圾对象占用的内存。
垃圾回收器有不同的实现策略,如标记-清除、引用计数、复制、标记-整理等。
除了以上三个主要的部分,JVM还包含其他组件,如堆内存、栈、方法区等。
堆内存用于存储对象实例,栈用于存储局部变量和方法调用参数,方法区用于存储类的元数据和静态数据。
JVM的工作过程如下:1. 通过类加载器加载Java字节码。
2.解释执行或JIT编译字节码。
3.根据需要进行垃圾回收和内存管理。
4.执行程序。
JVM的优点是跨平台性、自动内存管理和高性能。
通过JVM,Java程序可以在不同的硬件和操作系统上运行,无需修改源代码。
JVM的自动内存管理功能减轻了开发人员对内存管理的负担,避免了内存泄漏和越界访问等错误。
JVM的即时编译技术能够将热点代码优化为本地机器代码,提高程序的执行效率。
在实际的Java应用开发中,了解JVM的工作原理有助于编写高效的代码和解决性能问题。
开发人员可以通过调整JVM参数、选择合适的垃圾回收器和内存分配策略来优化程序的性能。
同时,了解JVM的工作原理还有助于理解虚拟机层面的问题和调优技巧,提升应用的可靠性和稳定性。
Java虚拟机JVM之server模式与client模式的区别

Java虚拟机JVM之server模式与client模式的区别JVM client模式和Server模式区别JVM Server模式与client模式启动,最主要的差别在于:-Server模式启动时,速度较慢,但是⼀旦运⾏起来后,性能将会有很⼤的提升。
JVM⼯作在Server模式下可以⼤⼤提⾼性能,Server模式下应⽤的启动速度会⽐client模式慢⼤概10%,但运⾏速度⽐Client VM要快⾄少有10倍当不指定运⾏模式参数时,虚拟机启动检测主机是否为服务器,如果是,则以Server模式启动,否则以client模式启动,J2SE5.0检测的根据是⾄少2个CPU和最低2GB内存由于服务器的CPU、内存和硬盘都⽐客户端机器强⼤,所以程序部署后,都应该以server模式启动,获取较好的性能;JVM在client模式默认-Xms是1M,-Xmx是64M;JVM在Server模式默认-Xms是128M,-Xmx是1024M;server:启动慢,编译更完全,编译器是⾃适应编译器,效率⾼,针对服务端应⽤优化,在服务器环境中最⼤化程序执⾏速度⽽设计。
client:快速启动,内存占⽤少,编译快,针对桌⾯应⽤程序优化,为在客户端环境中减少启动时间⽽优化;当JVM⽤于启动GUI界⾯的交互应⽤时适合于使⽤client模式,当JVM⽤于运⾏服务器后台程序时建议⽤Server模式。
我们可以通过运⾏:java -version来查看jvm默认⼯作在什么模式。
关于图⼀中的GCCclien模式下,新⽣代选择的是串⾏gc,旧⽣代选择的是串⾏gcserver模式下,新⽣代选择的是并⾏回收gc,旧⽣代选择的是并⾏gc⼀般来说我们系统应⽤选择有两种⽅式:吞吐量优先和暂停时间优先,对于吞吐量优先的采⽤server默认的并⾏gc⽅式,对于暂停时间优先的选⽤并发gc(CMS)⽅式。
其它延伸知识点JDK有两种VM,VM客户端,VM服务器应⽤程序。
java虚拟机环境配置

文章分类:Java编程第一步:下载jdk和tomcat:JDK下载Tomcat下载最新的jdk为1.6.10,tomcat为6.0,建议jdk1.4以上,tomcat4.0以上第二步:安装和配置你的jdk和tomcat:执行jdk和tomcat的安装程序,然后设置按照路径进行安装即可。
1.安装jdk以后,需要配置一下环境变量,在我的电脑->属性->高级->环境变量->系统变量中添加以下环境变量(假定你的jdk安装在C:\Program Files\Java):JAVA_HOME=C:\Program Files\Java\jdk1.6.0_10classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;(.;一定不能少,因为它代表当前路径)path=%JAVA_HOME%\bin接着可以写一个简单的java程序来测试JDK是否已安装成功:Java代码1.public class Test{2.3.public static void main(String args[]){4.5.System.out.println("This is a test program.");6.7.}8.9.}将上面的这段程序保存为文件名为Test.java的文件。
然后打开命令提示符窗口,cd到你的Test.java所在目录,然后键入下面的命令javac Test.javajava Test此时如果看到打印出来This is a test program.的话说明安装成功了,如果没有打印出这句话,你需要仔细检查一下你的配置情况。
2.安装Tomcat后,在我的电脑->属性->高级->环境变量->系统变量中添加以下环境变量(假定你的tomcat安装在c:\tomcat):CATALINA_HOME:C:\Program Files\Apache Software Foundation\Tomcat 6.0CATALINA_BASE:C:\Program Files\Apache Software Foundation\Tomcat 6.0TOMCAT_HOME: C:\Program Files\Apache Software Foundation\Tomcat 6.0然后修改环境变量中的classpath,把tomat安装目录下的common\lib下的servlet.jar 追加到classpath中去,修改后的classpath如下:classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME% \common\lib\servlet-api.jar;【注意最新版本的Tomcat中可能没有common这个文件夹了。
深入理解java虚拟机

深入理解java虚拟机(一)虚拟机内存划分Java虚拟机在执行Java程序时,会把它管理的内存划分为若干个不同的数据区。
这些区域有不同的特性,起不同的作用。
它们有各自的创建时间,销毁时间。
有的区域随着进程的启动而创建,随着进程结束而销毁,有的则始终贯穿虚拟机整个生命周期。
Java虚拟机运行时内存区域主要分为七部分,分别是:程序计数器,Java虚拟机栈,本地方法栈,方法区,Java堆,运行时常量池,直接内存。
如上图所示(图片来源于网络):蓝色区域包裹的部分为运行时几个数据区域:白色的部分为线程私有的,既随着线程的启动而创建。
每个线程都拥有各自的一份内存区域。
它们是:JAVA栈(JAVA STACK),本地方法栈(NATIVE METHOD STACK),和程序计数器(PROGRAM COUNTER REGISTER)。
黄色部分是线程共享的,所有的线程共享该区域的内容。
他们是:方法区(METHOD AREA),堆(HEAP)。
我们分别来介绍这些区域。
(1)程序计数器(program counter register)学过计算机组成原理的都知道计算机处理器中的程序计数器。
当处理器执行一条指令时,首先需要根据PC中存放的指令地址,将指令由内存取到指令寄存器中,此过程称为“取指令”。
与此同时,PC中的地址或自动加1或由转移指针给出下一条指令的地址。
此后经过分析指令,执行指令。
完成第一条指令的执行,而后根据PC取出第二条指令的地址,如此循环,执行每一条指令。
处理器的程序计数器是指寄存器,而java程序计数器是指一小块内存空间。
java代码编译字节码之后,虚拟机会一行一行的解释字节码,并翻印成本地代码。
这个程序计数器盛放的就是当前线程所执行字节码的行号的指示器。
在虚拟机概念模型中,字节码解释器工作室就是通过改变这个计数器的值来选取下一条需要执行的字节码指令,分支,循环,跳转,异常处理等都依赖于它。
Java虚拟机的多线程是通过线程轮流切换并分配处理器执行时间的方式实现的,因此为了线程切换后还能恢复执行位置,每条线程都需要一个独立的程序计数器。
Java命令详解
Java命令详解Java是通过java虚拟机来装载和执⾏编译⽂件(class⽂件)的,java虚拟机通过命令java option 来启动,-option为虚拟机参数,通过这些参数可对虚拟机的运⾏状态进⾏调整. ⼀、如何查看参数列表: 虚拟机参数分为基本和扩展两类,在命令⾏中输⼊java 可得到基本参数列表,java X则可得到扩展参数列表。
注:以上假设已经把JAVA_HOME\bin路径加⼊到了path路径 ⼆、基本参数说明: 1. -classpath,-cp 虚拟机在运⾏⼀个类时,需要将其装⼊内存,虚拟机搜索类的⽅式和顺序如下: Bootstrap classes,Extension classes,User classes。
Bootstrap中的路径是虚拟机⾃带的jar或zip⽂件,虚拟机⾸先搜索这些包⽂件,⽤System.getProperty("sun.boot.class.path")可得到虚拟机搜索的包名。
Extension是位于jre"lib"ext⽬录下的jar⽂件,虚拟机在搜索完Bootstrap后就搜索该⽬录下的jar⽂件。
⽤System. getProperty("java.ext.dirs”)可得到虚拟机使⽤Extension搜索路径。
User classes搜索顺序为当前⽬录、环境变量 CLASSPATH、-classpath。
-cp 和 -classpath 是同义词,参数意义是⼀样的。
classpath参数太长了,所以提供cp作为缩写形式,它们⽤于告知虚拟机搜索⽬录名、jar⽂档名、zip⽂档名,之间⽤分号;分隔。
例如当你⾃⼰开发了公共类并包装成⼀个common.jar包,在使⽤ common.jar中的类时,就需要⽤-classpath common.jar 告诉虚拟机从common.jar中查找该类,否则虚拟机就会抛出ng.NoClassDefFoundError异常,表明未找到类定义。
深入剖析java虚拟机源码剖析与实例详解
深入剖析java虚拟机源码剖析与实例详解《深入剖析Java虚拟机源码剖析与实例详解》一书是近年来Java虚拟机领域的一本经典著作。
本文将从四个方面对这本书进行详细的剖析和评价。
一、内容概述本书主要从解读Java虚拟机的本质来讲解Java虚拟机,全书共分八个章节,内容分为虚拟机概述、运行时内存区域、执行引擎、类加载机制、字节码指令集和类文件结构六大块。
作者通过对Java虚拟机的实现原理剖析和对源代码关键代码的解析,帮助Java开发人员和技术爱好者更好地理解Java虚拟机的实现机制。
二、内容分析1. 虚拟机概述本章主要是介绍了Java虚拟机的工作原理、Java虚拟机与JDK、JRE的关系等,也阐述了一些JVM的基本概念和相应的JVM命令,如应用程序、类、对象、内存划分、垃圾回收、线程、栈等。
2. 运行时内存区域本章主要介绍Java虚拟机运行时内存的区域,分为线程共享区、堆、方法区、程序计数器,其中程序计数器是一种较为特殊的内存区域,主要用来记录线程正在执行的字节码指令的地址。
3. 执行引擎本章主要介绍Java虚拟机的执行引擎,执行引擎是JVM最具有特色的功能之一,主要起到将字节码翻译成具体的硬件指令的作用。
4. 类加载机制本章主要介绍Java类加载的机制,从类文件的加载、连接和初始化这三个阶段来解析。
5. 字节码指令集本章主要介绍了Java字节码指令集,这是Java虚拟机实现的关键之一。
字节码指令集是一个指令集,主要用来描述Java程序的行为。
6. 类文件结构本章主要介绍了Java类文件的内部结构,包括常量池、类信息、字段、方法等内容。
这些知识对于理解Java 虚拟机内部的运行机制和机制实现是非常有帮助的。
三、优点分析1. 按照设计和实现原理展开本书以Java虚拟机的设计原理和实现机制为主干,一步步推展,非常有条理和逻辑。
仔细分析每个过程,熟悉JVM的理论知识,加深JVM使用的理解,促进JVM的实际应用。
Java虚拟机(JVM)面试题-51道
4. 说一下 JVM由那些部分组成,运行流程是什么?JVM包含两个子系统和两个组件: 两个子系统为Class loader(类装载)、Execution engine(执行引擎);两个组件为Runtime data area(运行时数据区)、Native Interface(本地接口)。
Class loader(类装载):根据给定的全限定名类名(如:ng.Object)来装载class文件到Runtime data area中的method area。
Execution engine(执行引擎):执行classes中的指令。
Native Interface(本地接口):与native libraries交互,是其它编程语言交互的接口。
Runtime data area(运行时数据区域):这就是我们常说的JVM的内存。
程序计数器(Program Counter Register):当前线程所执行的字节码的行号指示器,字节码解线程A在看直播突然,线程B来了一个视频电话,就会抢夺线程A的时间片,就会打断了线程A,线程A 就会挂起解析栈帧:1. 局部变量表:是用来存储我们临时8个基本数据类型、对象引用地址、returnAddress类型。
(returnAddress中保存的是return后要执行的字节码的指令地址。
)2. 操作数栈:操作数栈就是用来操作的,例如代码中有个 i = 6*6,他在一开始的时候就会进行操作,读取我们的代码,进行计算后再放入局部变量表中去3. 动态链接:假如我方法中,有个 service.add()方法,要链接到别的方法中去,这就是动态链接,存储链接的地方。
4. 出口:出口是什呢,出口正常的话就是return 不正常的话就是抛出异常落一个方法调用另一个方法,会创建很多栈帧吗?答:会创建。
如果一个栈中有动态链接调用别的方法,就会去创建新的栈帧,栈中是由顺序的,一个栈帧调用另一个栈帧,另一个栈帧就会排在调用者下面栈指向堆是什么意思?栈指向堆是什么意思,就是栈中要使用成员变量怎么办,栈中不会存储成员变量,只会存储一个应用地址递归的调用自己会创建很多栈帧吗?答:递归的话也会创建多个栈帧,就是在栈中一直从上往下排下去8. 你能给我详细的介绍Java堆吗?(重点理解)java堆(Java Heap)是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域,在虚拟机启动时创建。
java虚拟机安装操作手册
JA V A虚拟机安装操作手册目录一、JA V A虚拟机的安装 (1)二、安装JAVA虚拟机注意事项 (2)三、如何查看JAVA虚拟机是否安装成功? (3)四、如何更新管理加载项 (4)五、常见问题 (6)一、JAVA虚拟机的安装如果您是第一次访问网上办税系统,用户操作时系统将提示您下载安装JA V A虚拟机。
如果您已经使用了其他虚拟机(如微软虚拟机),在访问网上办税系统时出现网页错误、无法提交数据等错误,请卸载后下载安装本系统提供的JA V A虚拟机。
下载地址在:福建省国家税务局首页-办税指南-下载中心-税务软件-网上办税JA V A虚拟机下载-下载JA V A虚拟机安装软件。
下载完成后,双击下载的文件“j2re-1_4_0-win-i.exe”,按默认点击“下一步”安装;请确认勾选“Microsoft Internet Explorer”,安装完成后请重开IE浏览器或重启电脑。
【备注】:二、安装JAVA虚拟机注意事项①请确认操作系统是WIN2000以上,并且IE的版本是6.0;②如有安装JA V A程序,请检查所使用的操作系统的登录帐号是否以中文命名,如是,请将JA V A程序卸载后新建一个以英文命名的帐号再进行安装;③卸载了JA V A虚拟机后重新安装,并且一定要将该程序安装在默认的目录下,安装后需重新打开浏览器再进行操作或重启电脑;④如有安装防火墙或卡巴斯基等杀毒软件请先将其关闭再安装;⑤如果安装JA V A虚拟机之后无法打开办税大厅页面,您有可能安装“上网助手”、“雅虎助手”、“中文上网”等辅助软件,阻止了JA V A虚拟机的运行,请删除类似辅助软件。
三、如何查看JAVA虚拟机是否安装成功?①打开IE浏览器,点击“工具”->“Internet选项(O)...”②选中“高级”,查看是否有下图中红色框选中的信息,表示已安装成功,③没有安装成功则如下图所示,请您卸载后重新安装。
四、如何更新管理加载项(1)任意打开一个网页。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
深入理解JVM1 Java技术与Java虚拟机说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成: Java编程语言、Java类文件格式、Java虚拟机和Java应用程序接口(Java API)。
它们的关系如下图所示:图1 Java四个方面的关系运行期环境代表着Java平台,开发人员编写Java代码(.java文件),然后将之编译成字节码(.class文件)。
最后字节码被装入内存,一旦字节码进入虚拟机,它就会被解释器解释执行,或者是被即时代码发生器有选择的转换成机器码执行。
从上图也可以看出Java平台由Java虚拟机和Java应用程序接口搭建,Java 语言则是进入这个平台的通道,用Java语言编写并编译的程序可以运行在这个平台上。
这个平台的结构如下图所示:在Java平台的结构中, 可以看出,Java虚拟机(JVM) 处在核心的位置,是程序与底层操作系统和硬件无关的关键。
它的下方是移植接口,移植接口由两部分组成:适配器和Java操作系统, 其中依赖于平台的部分称为适配器;JVM 通过移植接口在具体的平台和操作系统上实现;在JVM 的上方是Java的基本类库和扩展类库以及它们的API,利用Java API编写的应用程序(application) 和小程序(Java applet) 可以在任何Java平台上运行而无需考虑底层平台, 就是因为有Java虚拟机(JVM)实现了程序与操作系统的分离,从而实现了Java 的平台无关性。
那么到底什么是Java虚拟机(JVM)呢?通常我们谈论JVM时,我们的意思可能是:1. 对JVM规范的的比较抽象的说明;2. 对JVM的具体实现;3. 在程序运行期间所生成的一个JVM实例。
对JVM规范的的抽象说明是一些概念的集合,它们已经在书《The Java Virtual Machine Specification》(《Java虚拟机规范》)中被详细地描述了;对JVM的具体实现要么是软件,要么是软件和硬件的组合,它已经被许多生产厂商所实现,并存在于多种平台之上;运行Java程序的任务由JVM的运行期实例单个承担。
在本文中我们所讨论的Java虚拟机(JVM)主要针对第三种情况而言。
它可以被看成一个想象中的机器,在实际的计算机上通过软件模拟来实现,有自己想象中的硬件,如处理器、堆栈、寄存器等,还有自己相应的指令系统。
JVM在它的生存周期中有一个明确的任务,那就是运行Java程序,因此当Java程序启动的时候,就产生JVM的一个实例;当程序运行结束的时候,该实例也跟着消失了。
下面我们从JVM的体系结构和它的运行过程这两个方面来对它进行比较深入的研究。
2 Java虚拟机的体系结构刚才已经提到,JVM可以由不同的厂商来实现。
由于厂商的不同必然导致JVM在实现上的一些不同,然而JVM还是可以实现跨平台的特性,这就要归功于设计JVM时的体系结构了。
我们知道,一个JVM实例的行为不光是它自己的事,还涉及到它的子系统、存储区域、数据类型和指令这些部分,它们描述了JVM的一个抽象的内部体系结构,其目的不光规定实现JVM时它内部的体系结构,更重要的是提供了一种方式,用于严格定义实现时的外部行为。
每个JVM都有两种机制,一个是装载具有合适名称的类(类或是接口),叫做类装载子系统;另外的一个负责执行包含在已装载的类或接口中的指令,叫做运行引擎。
每个JVM又包括方法区、堆、Java栈、程序计数器和本地方法栈这五个部分,这几个部分和类装载机制与运行引擎机制一起组成的体系结构图为:图3 JVM的体系结构JVM的每个实例都有一个它自己的方法域和一个堆,运行于JVM内的所有的线程都共享这些区域;当虚拟机装载类文件的时候,它解析其中的二进制数据所包含的类信息,并把它们放到方法域中;当程序运行的时候,JVM把程序初始化的所有对象置于堆上;而每个线程创建的时候,都会拥有自己的程序计数器和Java栈,其中程序计数器中的值指向下一条即将被执行的指令,线程的Java栈则存储为该线程调用Java 方法的状态;本地方法调用的状态被存储在本地方法栈,该方法栈依赖于具体的实现。
下面分别对这几个部分进行说明。
执行引擎处于JVM的核心位置,在Java虚拟机规范中,它的行为是由指令集所决定的。
尽管对于每条指令,规范很详细地说明了当JVM执行字节码遇到指令时,它的实现应该做什么,但对于怎么做却言之甚少。
Java虚拟机支持大约248个字节码。
每个字节码执行一种基本的CPU运算,例如,把一个整数加到寄存器,子程序转移等。
Java指令集相当于Java程序的汇编语言。
Java指令集中的指令包含一个单字节的操作符,用于指定要执行的操作,还有0个或多个操作数,提供操作所需的参数或数据。
许多指令没有操作数,仅由一个单字节的操作符构成。
虚拟机的内层循环的执行过程如下:do{取一个操作符字节;根据操作符的值执行一个动作;}while(程序未结束)由于指令系统的简单性,使得虚拟机执行的过程十分简单,从而有利于提高执行的效率。
指令中操作数的数量和大小是由操作符决定的。
如果操作数比一个字节大,那么它存储的顺序是高位字节优先。
例如,一个16位的参数存放时占用两个字节,其值为:第一个字节*256+第二个字节字节码。
指令流一般只是字节对齐的。
指令tableswitch和lookup是例外,在这两条指令内部要求强制的4字节边界对齐。
对于本地方法接口,实现JVM并不要求一定要有它的支持,甚至可以完全没有。
Sun公司实现Java本地接口(JNI)是出于可移植性的考虑,当然我们也可以设计出其它的本地接口来代替Sun公司的JNI。
但是这些设计与实现是比较复杂的事情,需要确保垃圾回收器不会将那些正在被本地方法调用的对象释放掉。
Java的堆是一个运行时数据区,类的实例(对象)从中分配空间,它的管理是由垃圾回收来负责的:不给程序员显式释放对象的能力。
Java不规定具体使用的垃圾回收算法,可以根据系统的需求使用各种各样的算法。
Java方法区与传统语言中的编译后代码或是Unix进程中的正文段类似。
它保存方法代码(编译后的java代码)和符号表。
在当前的Java实现中,方法代码不包括在垃圾回收堆中,但计划在将来的版本中实现。
每个类文件包含了一个Java类或一个Java界面的编译后的代码。
可以说类文件是Java语言的执行代码文件。
为了保证类文件的平台无关性,Java虚拟机规范中对类文件的格式也作了详细的说明。
其具体细节请参考Sun公司的Java 虚拟机规范。
Java虚拟机的寄存器用于保存机器的运行状态,与微处理器中的某些专用寄存器类似。
Java虚拟机的寄存器有四种:1. pc: Java程序计数器;2. optop: 指向操作数栈顶端的指针;3. frame: 指向当前执行方法的执行环境的指针;。
4. vars: 指向当前执行方法的局部变量区第一个变量的指针。
在上述体系结构图中,我们所说的是第一种,即程序计数器,每个线程一旦被创建就拥有了自己的程序计数器。
当线程执行Java方法的时候,它包含该线程正在被执行的指令的地址。
但是若线程执行的是一个本地的方法,那么程序计数器的值就不会被定义。
Java虚拟机的栈有三个区域:局部变量区、运行环境区、操作数区。
局部变量区每个Java方法使用一个固定大小的局部变量集。
它们按照与vars寄存器的字偏移量来寻址。
局部变量都是32位的。
长整数和双精度浮点数占据了两个局部变量的空间,却按照第一个局部变量的索引来寻址。
(例如,一个具有索引n的局部变量,如果是一个双精度浮点数,那么它实际占据了索引n和n+1所代表的存储空间)虚拟机规范并不要求在局部变量中的64位的值是64位对齐的。
虚拟机提供了把局部变量中的值装载到操作数栈的指令,也提供了把操作数栈中的值写入局部变量的指令。
运行环境区在运行环境中包含的信息用于动态链接,正常的方法返回以及异常捕捉。
动态链接运行环境包括对指向当前类和当前方法的解释器符号表的指针,用于支持方法代码的动态链接。
方法的class文件代码在引用要调用的方法和要访问的变量时使用符号。
动态链接把符号形式的方法调用翻译成实际方法调用,装载必要的类以解释还没有定义的符号,并把变量访问翻译成与这些变量运行时的存储结构相应的偏移地址。
动态链接方法和变量使得方法中使用的其它类的变化不会影响到本程序的代码。
正常的方法返回如果当前方法正常地结束了,在执行了一条具有正确类型的返回指令时,调用的方法会得到一个返回值。
执行环境在正常返回的情况下用于恢复调用者的寄存器,并把调用者的程序计数器增加一个恰当的数值,以跳过已执行过的方法调用指令,然后在调用者的执行环境中继续执行下去。
异常捕捉异常情况在Java中被称作Error(错误)或Exception(异常),是Throwable类的子类,在程序中的原因是:①动态链接错,如无法找到所需的class文件。
②运行时错,如对一个空指针的引用。
程序使用了throw语句。
当异常发生时,Java虚拟机采取如下措施:∙检查与当前方法相联系的catch子句表。
每个catch子句包含其有效指令范围,能够处理的异常类型,以及处理异常的代码块地址。
∙与异常相匹配的catch子句应该符合下面的条件:造成异常的指令在其指令范围之内,发生的异常类型是其能处理的异常类型的子类型。
如果找到了匹配的catch子句,那么系统转移到指定的异常处理块处执行;如果没有找到异常处理块,重复寻找匹配的catch子句的过程,直到当前方法的所有嵌套的catch子句都被检查过。
∙由于虚拟机从第一个匹配的catch子句处继续执行,所以catch子句表中的顺序是很重要的。
因为Java 代码是结构化的,因此总可以把某个方法的所有的异常处理器都按序排列到一个表中,对任意可能的程序计数器的值,都可以用线性的顺序找到合适的异常处理块,以处理在该程序计数器值下发生的异常情况。
∙如果找不到匹配的catch子句,那么当前方法得到一个"未截获异常"的结果并返回到当前方法的调用者,好像异常刚刚在其调用者中发生一样。
如果在调用者中仍然没有找到相应的异常处理块,那么这种错误将被传播下去。
如果错误被传播到最顶层,那么系统将调用一个缺省的异常处理块。
操作数栈区机器指令只从操作数栈中取操作数,对它们进行操作,并把结果返回到栈中。
选择栈结构的原因是:在只有少量寄存器或非通用寄存器的机器(如Intel486)上,也能够高效地模拟虚拟机的行为。
操作数栈是32位的。
它用于给方法传递参数,并从方法接收结果,也用于支持操作的参数,并保存操作的结果。
例如,iadd指令将两个整数相加。
相加的两个整数应该是操作数栈顶的两个字。