Jdk Jre Jvm.docx

  • Uploaded by: Vijay Akula
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Jdk Jre Jvm.docx as PDF for free.

More details

  • Words: 9,212
  • Pages: 47
https://howtodoinjava.com/java/basics/jdk-jre-jvm/

What is Java JDK, JRE and JVM – In-depth Analysis By Lokesh Gupta | Filed Under: Java Basics

Learn the differences between JDK, JRE and JVM. How JVM works inside? What are class loaders, interpreter and JIT compilers. Also checkout some interview questions. Table of Contents 1. Execution of a Java Program 2. What is JVM? 3. What is JRE? 4. What is JDK? 5. Differences between JDK, JRE and JVM 6. Interview questions related to JDK, JRE and JVM 7. JDK and JRE downloads

1. Execution of a Java Program Before jumping into the internals of Java, let’s understand how a Java source file is executed. 1. We write the Java source code in Simple.Java file using an editor or IDE (integrated development environment) e.g. Eclipse or IntelliJ Idea. 2. Program has to be compiled into bytecode. Java compiler (javac) compiles the sourcecode to Simple.class file. 3. This class file can be executed in any platform/OS by JVM (Java virtual machine). 4. JVM translates bytecode into native machine code which machines can execute.

Java Execution Flow

2. What is JVM? Java Virtual machine (JVM) is the virtual machine that runs the Java bytecodes. You get this bytecode by compiling the .java files into .class files. .class files contain the bytecodes understood by the JVM. In the real world, JVM is a specification that provides a runtime environment in which Java bytecode can be executed. Different vendors provide different implementations of this specification. For example, this wiki page lists down different JVM implementations. Most popular implementation of JVM is Hotspot which is owned and provided by Oracle Corporation. (Previously by Sun Microsystems, Inc.). JVM delivers the optimal performance for Java applications using many advanced techniques, incorporating a state-of-the-art memory model, garbage collector, and adaptive optimizer. JVM comes in two different flavors – client and server. Although the Server and the Client VMs are similar, the Server VM has been specially tuned to maximize peak operating speed. It is intended for executing long-running server applications, which need the fastest possible operating speed more than a fast start-up time or smaller runtime memory footprint. Developers can choose which system they want by specifying -client or -server. The JVM is called virtual because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This

independence from hardware and the operating system is a cornerstone of the writeonce-run-anywhere value of Java programs.

2.1. JVM Architecture

JVM Architecture 2.1.1. Class Loader The class loader is a subsystem used for loading class files. It performs three major functions i.e. class loading, linking, and initialization. 1. Loading 

To load classes, JVM has 3 kind of class loaders. Bootstrap, extension and application class loader.



When loading a class file, JVM finds out a dependency for some arbitrary class XYZ.class.



First bootstrap class loader tries to find the class. It scans the rt.jar file in JRE lib folder.



If class is not found then extension class loader searches the class file in inside jre\lib\ext folder.



Again if class is not found then application classloader searches all the Jar files and classes in CLASSPATH environment variable of system.



If class is found by any loader then class is loaded by class loader; else ClassNotFoundException is thrown.

2. Linking After class is loaded by the classloader, linking is performed. A bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get a verification error. It also performs the memory allocation to static variables and methods found in the class. 3. Initialization This is the final phase of class loading, here all static variable will be assigned with the original values and the static blocks will be executed. 2.1.2. JVM Memory Areas Memory area inside JVM is divided into multiple parts to store specific parts of application data. 

Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.



Heap stores all objects that are created during application execution.



Stacks store local variables, and intermediate results. All such variables are local to the thread by which they are created. Each thread has its own JVM stack, created simultaneously as the thread is created. So all such local variable are called thread-local variables.



PC register store the physical memory address of the statements which is currently executing. In Java, each thread has its separate PC register.



Java supports and uses native code as well. Many low level code is written in languages like C and C++. Native method stacks hold the instruction of native code.

2.2. JVM Execution Engine

All code assigned to JVM is executed by an execution engine. The execution engine reads the byte code and executes one by one. It uses two inbuilt interpreter and JIT compiler to convert the bytecode to machine code and execute it.

Platform Specific Interpreters With JVM, both interpreter and compiler produce native code. The difference is in how they generate the native code, how optimized it is as well how costly the optimization is.

2.2.1. Interpreter A JVM interpreter pretty much converts each byte-code instruction to corresponding native instruction by looking up a predefined JVM-instruction to machine instruction mapping. It directly executes the bytecode and does not perform any optimization.

2.2.2. JIT Compiler To improve performance, JIT compilers interact with the JVM at runtime and compile appropriate bytecode sequences into native machine code. Typically, JIT compiler takes a block of code (not one statement at a time as interpreter), optimize the code and then translate it to optimized machine code. The JIT compiler is enabled by default. You can disable the JIT compiler, in which case the entire Java program will be interpreted. Disabling the JIT compiler is not recommended except to diagnose or work around JIT compilation problems.

3. What is JRE?

The Java Runtime Environment (JRE) is a software package which bundles the libraries (jars) and the Java Virtual Machine, and other components to run applications written in the Java. JVM is just a part of JRE distributions. To execute any Java application, you need JRE installed in the machine. It’s minimum requirement to execute Java applications on any machine. JRE bundles the following components – 1. DLL files used by the Java HotSpot Client Virtual Machine. 2. DLL files used by the Java HotSpot Server Virtual Machine. 3. Code libraries, property settings, and resource files used by the Java runtime environment. e.g. rt.jar and charsets.jar. 4. Java extension files such as localedata.jar. 5. Contains files used for security management. These include the security policy(java.policy) and security properties (java.security) files. 6. Jar files containing support classes for applets. 7. Contains TrueType font files for use by the platform. JREs can be downloaded as part of JDKs or you can download them separately. JREs are platform dependent. It means that based on the type of machine (OS and architecture), you will have to select the JRE bundle to import and install. For example, you cannot install a 64-bit JRE distribution on 32-bit machine. Similarly, JRE distribution for Windows will not work in Linux; and vice-versa.

4. What is JDK? JDK is a superset of JRE. JDK contains everything that JRE has along with development tools for developing, debugging, and monitoring Java applications. You need JDK when you need to develop Java applications. Few important components shipped with JDKs are as follows: 

appletviewer – this tool can be used to run and debug Java applets without a web browser



apt – the annotation-processing tool



extcheck – a utility that detects JAR file conflicts



javadoc – the documentation generator, which automatically generates documentation from source code comments



jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files



jarsigner – the jar signing and verification tool



javap – the class file disassembler



javaws – the Java Web Start launcher for JNLP applications



JConsole – Java Monitoring and Management Console



jhat – Java Heap Analysis Tool



jrunscript – Java command-line script shell



jstack – utility that prints Java stack traces of Java threads



keytool – tool for manipulating the keystore



policytool – the policy creation and management tool



xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema and generates Java classes

Same as JREs, JDKs are also platform dependent. So take care when you download the JDK package for your machine.

5. Differences between JDK, JRE and JVM Based on the above discussions, we can draw a relationship between these three as below – JRE = JVM + libraries to run Java application. JDK = JRE + tools to develop Java Application.

JDK vs JRE vs JVM In short, if you are a Java application developer who writes code, you will need JDK installed in your machine. But, if you only want to run applications built in Java, you only need JRE installed into your machine.

6. Interview questions related to JDK, JRE and JVM If you understood whatever we have discussed so far in this post, then facing any interview question will not be difficult. Still, be prepared to answer questions like below:

1. What is JVM architecture? It’s already explained in detail.

2. How many types of class loaders are in Java?

There are 3 class loaders. Bootstrap, extension and application class loaders.

3. How class loader works in Java? Class loaders scan their pre-defined locations for jar files and classes. They scan all those class files in the path and look for the required class. If found they load, link and initialize the class file.

4. Difference between JRE and JVM? JVM is the specification for runtime environment which executes the Java applications. Hotspot JVM is such one implementation of the specification. It loads the class files and uses interpreter and JIT compiler to convert bytecode into machine code and execute it.

5. Difference between interpreter and JIT compiler? The interpreter interprets the bytecode line by line and executes it sequentially. It results in poor performance. JIT compiler add optimization to this process by analyzing the code in blocks and then prepare more optimized machine code.

7. JDK and JRE downloads You can find the platform-specific JDK and JRE software packages in Oracle’s Java distribution pages. For example, this page lists down all available JDK distributions for Java 8.

JDK 8 Distributions Similarly, JRE 8 distributions are available in this page.

JRE 8 Distributions Happy Learning !!

Further reading: How the JIT compiler optimizes code JIT compilation Understanding JIT compiler JDK and JRE File Structure

https://www.javaworld.com/article/3347596/jvm-jdk-jre-whats-the-difference.html

JVM, JDK, JRE: What's the difference? Three indispensable Java platform components, and how they work together in your Java applications 











By JavaWorld staff and Matthew Tyson JavaWorld | MARCH 11, 2019 11:57 AM PT

IDG / Oracle / Vasabii / Getty Images MORE LIKE THIS



What is the JRE? Introduction to the Java Runtime Environment



Choosing your Java IDE



What is the JDK? Introduction to the Java Development Kit Developers new to Java often wonder what differentiates the Java Virtual Machine, the Java Development Kit, and the Java Runtime Environment. They're also curious how these three Java platform components work together in Java applications. Finally, developers need to know how they will interact with each component. Briefly:   

The JVM is the Java platform component that executes your programs. The JRE creates the JVM and ensures dependencies are available to your programs. The JDK allows you to create Java programs that can be executed and run by the JVM and JRE. As a developer, you'll work with the JDK to write your applications and with the JVM to debug and optimize them, especially for performance. The JRE mostly runs in the background, but you may use it for application monitoring and memory configuration. That's the overview, but there's a lot more to know about each platform component. We've pulled it all together in three quick tutorials. Downloading and installing the JVM, JDK, and JRE Anytime you download a JDK, it will include a version-compatible JRE, and that JRE will include a default JVM. You also can download the JRE separately from the JDK, and you may choose from a variety of JVMs.

Vasabii / Getty Images

PART 1: What is the Java Virtual Machine? Technically, the JVM is a specification, describing the requirements for building a piece of software. From a developer's perspective, it's how we run our Java programs. You can choose from a variety of JVM implementations, and it's important to know how to load and execute class files using the JVM you choose. You also should know how to check and tune JVM memory usage. Learn more ...

Vasabii / Getty Images

PART 2: What is the Java Development Kit? The JDK is the package of tools you need to develop Java-based software. There isn't just one JDK, though, and it's important to know how to choose and download the correct JDK for the type of application you want to create. Learn more ...

Vasabii / Getty Images

PART 3: What is the Java Runtime Environment? The JRE is the runtime environment for Java, which means it's a piece of software that is designed to run Java code. While the JRE mostly runs in the background, it's important to know how to use JRE flags and switches to configure application memory. Learn more ...

https://www.javaworld.com/article/3272244/what-is-the-jvm-introducing-the-java-virtualmachine.html

What is the JVM? Introducing the Java Virtual Machine The JVM manages system memory and provides a portable execution environment for Javabased applications 











By Matthew Tyson JavaWorld | MAY 22, 2018 12:32 PM PT

IDG / Oracle / Vasabii / Getty Images MORE LIKE THIS  

JVM performance optimization, Part 1: A JVM technology primer Java 101: Trash talk, Part 1



Beyond Java: Programming languages on the JVM RELATED ARTICLES



20 practical Python libraries for every Python programmer



CI/CD your way: 11 on-prem options for continuous...



Review: Google Cloud AutoML is truly automated machine... See all Insider

The Java Virtual Machine is a program whose purpose is to execute other programs. It's a simple idea that also stands as one of our greatest examples of coding kung fu. The JVM upset the status quo for its time, and continues to support programming innovation today.

Use and definitions for the JVM The JVM has two primary functions: to allow Java programs to run on any device or operating system (known as the "Write once, run anywhere" principle), and to manage and optimize program memory. When Java was released in 1995, all computer programs were written to a specific operating system, and program memory was managed by the software developer. So the JVM was a revelation.

JavaWorld / IDG

Figure 1: High-level view of the JVM Having a technical definition for the JVM is useful, and there's also an everyday way that software developers think about it. Let's break those down:  

Technical definition: The JVM is the specification for a software program that executes code and provides the runtime environment for that code. Everyday definition: The JVM is how we run our Java programs. We configure the JVM's settings and then rely on it to manage program resources during execution.

When developers talk about the JVM, we usually mean the process running on a machine, especially a server, that represents and controls resource usage for a Java app. Contrast this to the JVM specification, which describes the requirements for building a program that performs these tasks. Who develops and and maintains the JVM? The JVM is widely deployed, heavily used, and maintained by some very bright programmers, both corporate and open source. The OpenJDK project is the offspring of the Sun Microsystems decision to open-source Java. OpenJDK has continued through Oracle's stewardship of Java, with much of the heavy lifting these days done by Oracle engineers.

Memory management in the JVM The most common interaction with a running JVM is to check the memory usage in the heap and stack. The most common adjustment is tuning the JVM's memory settings. Garbage collection Before Java, all program memory was managed by the programmer. In Java, program memory is managed by the JVM. The JVM manages memory through a process called garbage collection, which continuously identifies and eliminates unused memory in Java programs. Garbage collection happens inside a running JVM. In the early days, Java came under a lot of criticism for not being as "close to the metal" as C++, and therefore not as fast. The garbage collection process was especially controversial. Since then, a variety of algorithms and approaches have been proposed and used for garbage collection. With consistent development and optimization, garbage collection has vastly improved. [ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12part course! ] What does 'close to the metal' mean? When programmers say a programming language or platform is "close to the metal," we mean the developer is able to programmatically (by writing code) manage an operating system's memory. In theory, programmers can wring more performance out of our programs by stipulating how much is used and when to discard it. In most cases, delegating memory management to a highly refined process like the JVM yields better performance and fewer errors than doing it yourself.

The JVM in three parts It could be said there are three aspects to the JVM: specification, implementation and instance. Let's consider each of these.

1. The JVM specification First, the JVM is a software specification. In a somewhat circular fashion, the JVM spec highlights that its implementation details are not defined within the spec, in order to allow for maximum creativity in its realization: "To implement the Java virtual machine correctly, you need only be able to read the class file format and correctly perform the operations specified therein." J.S. Bach once described creating music similarly: "All you have to do is touch the right key at the right time." So, all the JVM has to do is run Java programs correctly. Sounds simple, might even look simple from outside, but it is a massive undertaking, especially given the power and flexibility of the Java language. The JVM as a virtual machine The JVM is a virtual machine that runs Java class files in a portable way. Being a virtual machine means the JVM is an abstraction of an underlying, actual machine--such as the server that your program is running on. Regardless of what operating system or hardware is actually present, the JVM creates a predictable environment for programs to run within. Unlike a true virtual machine, however, the JVM doesn't create a virtual operating system. It would be more accurate to describe the JVM as a managed runtime environment, or a process virtual machine. 2. JVM implementations Implementing the JVM specification results in an actual software program, which is a JVM implementation. In fact, there are many JVM implementations, both open source and proprietary. OpenJDK's HotSpot JVM is the reference implementation, and remains one of the most thoroughly tried-and-tested codebases in the world. HotSpot is also the most commonly used JVM. Almost all licensed JVM's are created as forks off the OpenJDK and the HotSpot JVM, including Oracle's licensed JDK. Developers creating a licensed fork off the OpenJDK are often motivated by the desire to add OS-specific performance improvements. Typically, you download and install the JVM as a bundled part of a Java Runtime Environment (JRE). 3. A JVM instance After the JVM spec has been implemented and released as a software product, you may download and run it as a program. That downloaded program is an instance (or instantiated version) of the JVM. Most of the time, when developers talk about "the JVM," we are referring to a JVM instance running in a software development or production environment. You might say, "Hey Anand, how much memory is the JVM on that server using?" or, "I can't believe I created a circular call and a stack overflow error crashed my JVM. What a newbie mistake!"

What is a software specification? A software specification (or spec) is a human-readable design document that describes how a software system should operate. The purpose of a specification is to create a clear description and requirements for engineers to code to.

Loading and executing class files in the JVM We've talked about the JVM's role in running Java applications, but how does it perform its function? In order to run Java applications, the JVM depends on the Java class loader and a Java execution engine. The Java class loader in the JVM Everything in Java is a class, and all Java applications are built from classes. An application could consist of one class or thousands. In order to run a Java application, a JVM must load compiled .class files into a context, such as a server, where they can be accessed. A JVM depends on its class loader to perform this function. The Java class loader is the part of the JVM that loads classes into memory and makes them available for execution. Class loaders use techniques like lazy-loading and caching to make class loading as efficient as it can be. That said, class loading isn't the epic brain-teaser that (say) portable runtime memory management is, so the techniques are comparatively simple. Every Java Virtual Machine includes a class loader. The JVM spec describes standard methods for querying and manipulating the class loader at runtime, but JVM implementations are responsible for fulfilling these capabilities. From the developer's perspective, the underlying class loader mechanisms are typically a black box. The execution engine in the JVM Once the class loader has done its work of loading classes, the JVM begins executing the code in each class. The execution engine is the JVM component that handles this function. The execution engine is essential to the running JVM. In fact, for all practical purposes, it is the JVM instance. Executing code involves managing access to system resources. The JVM execution engine stands between the running program--with its demands for file, network and memory resources--and the operating system, which supplies those resources. How the execution engine manages system resources System resources can be divided into two broad categories: memory and everything else. Recall that the JVM is responsible for disposing of unused memory, and that garbage collection is the mechanism that does that disposal. The JVM is also responsible for allocating and maintaining the referential structure that the developer takes for granted. As an example, the

JVM's execution engine is responsible for taking something like the new keyword in Java, and turning it into an OS-specific request for memory allocation. Beyond memory, the execution engine manages resources for file system access and network I/O. Since the JVM is interoperable across operating systems, this is no mean task. In addition to each application's resource needs, the execution engine must be responsive to each OS environment. That is how the JVM is able to handle in-the-wild demands.

JVM evolution: Past, present, future In 1995, the JVM introduced two revolutionary concepts that have since become standard fare for modern software development: "Write once, run anywhere" and automatic memory management. Software interoperability was a bold concept at the time, but few developers today would think twice about it. Likewise, whereas our engineering forebears had to manage program memory themselves, my generation grew up with garbage collection. We could say that James Gosling and Brendan Eich invented modern programming, but thousands of others have refined and built on their ideas over the following decades. Whereas the Java Virtual Machine was originally just for Java, today it has evolved to support many scripting and programming languages, including Scala, Groovy, and Kotlin. Looking forward, it's hard to see a future where the JVM isn't a prominent part of the development landscape. More JavaWorld tutorials about the JVM     

Trash talk, how Java recycles memory The lean, mean virtual machine Bytecode basics: A first look at bytecodes in the JVM JVM exceptions: How the JVM handles exceptions JVM performance optimization: A JVM technology primer

What is the JDK? Introduction to the Java Development Kit

The JDK is a key platform component for building Java applications. At its heart is the Java compiler 











By Matthew Tyson JavaWorld | AUGUST 09, 2018 01:20 PM PT

IDG / Oracle / Vasabii / Getty Images MORE LIKE THIS



Java 101: Learn Java from the ground up



What is the JRE? Introduction to the Java Runtime Environment



What is the JVM? Introducing the Java Virtual Machine RELATED ARTICLES



20 practical Python libraries for every Python programmer



CI/CD your way: 11 on-prem options for continuous...



Review: Google Cloud AutoML is truly automated machine... See all Insider The Java Development Kit (JDK) is one of three core technology packages used in Java programming, along with the JVM (Java Virtual Machine) and the JRE (Java Runtime Environment). It's important to differentiate between these three technologies, as well as understanding how they're connected:   

The JVM is the Java platform component that executes programs. The JRE is the on-disk part of Java that creates the JVM. The JDK allows developers to create Java programs that can be executed and run by the JVM and JRE. Developers new to Java often confuse the Java Development Kit and the Java Runtime Environment. The distinction is that the JDK is a package of tools for developing Java-based software, whereas the JRE is a package of tools for running Java code. The JRE can be used as a standalone component to simply run Java programs, but it's also part of the JDK. The JDK requires a JRE because running Java programs is part of developing them. Figure 1 shows how the JDK fits into the Java application development lifecycle.

Matthew Tyson

Figure 1. High-level view of the JDK Just as we did with my recent introduction to the Java Virtual Machine, let's consider the technical and everyday definitions of the JDK:  

Technical definition: The JDK is an implementation of the Java platform specification, including compiler and class libraries. Everyday definition: The JDK is a software package you download in order to create Java-based applications. The JDK & the Java compiler In addition to the JRE, which is the environment used to run Java applications, every JDK contains a Java compiler. The compiler is the software program capable of taking raw .java files--which are plain text--and rendering them into executable .class files. We'll see the compiler in action soon. First, I'll show you how to download and setup a JDK in your development environment.

Get started with the JDK Getting Java setup in your development environment is as easy as downloading a JDK and adding it to your classpath. When you download your JDK, you will need to select the version of Java you want to use. Java 8 is the version most commonly in use, but as of this writing Java 10 is the newest release. Java maintains backward compatibility, so we'll just download the latest release. JDK packages In addition to choosing your Java version, you will also need to select a Java package. Packages are Java Development Kits that are targeted for different types of development. The available packages are Java Enterprise Edition (Java EE), Java Standard Edition (Java SE), and Java Mobile Edition (Java ME). [ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12part course! ] Novice developers are sometimes unsure which package is correct for their project. Generally, each JDK version contains Java SE. If you download Java EE or Java ME, you will get the standard edition with it. For example, Jave EE is the standard platform with additional tools useful for enterprise application development such as Enterprise JavaBeans or support for Object Relational Mapping.

It's also not hard to switch to a different JDK in the future if you find you need to. Don't worry too much about choosing the correct Java version and JDK package when you are just starting out. JDK version compatibility Since the JDK supplies the compiler for your Java programs, the JDK you use determines what Java version you are able to code in. For example, if you want to use the newer functional support found in Java 8, like the arrow Lambda operator, then you need at least the Java 8 JDK for compiling. Otherwise, the javac command will reject the code with a syntax error.

Downloading the JDK We'll stick with Java SE for this tutorial, so that we can focus on the core JDK classes and technologies. To download the Java SE JDK, visit Oracle's official download page. You'll see the various JDK packages available, as shown in Figure 2.

Matthew Tyson

Figure 2. Available JDK packages Before you select the Java SE download, take a minute to look at the other options. There's a lot cooking in the Java kitchen! About Java EE You would download a Java EE JDK if you were primarily interested in building Java-based web applications. The Java EE JDK includes the Java Servlet specification, which supports HTTP request handling. Every Java EE JDK implementation also requires a container, which is a server

that runs Java EE applications. Glassfish is the Java EE server reference implementation for Oracle. Other popular implementations are Tomcat and Jetty. Go ahead and download the Java Standard Edition JDK. Options for that are shown in Figure 3.

Matthew Tyson

Figure 3. Available JDKs for download

Installing the JDK When you run the JDK installer, you are offered a selection of three components: Development Tools, Source Code, and Public JRE. You may install one or all of them. In this case, just select the default. Installing the "Development Tools" option gives you the JDK proper. Installing "Source Code" contains the sources for the public classes in the core Java API. Including this option allows you to reference the source code when building apps. The third option, "Public JRE," drives home that the JDK and JRE are separate entities: the public JRE can be used by other programs to execute Java programs, and can be installed separately from the JDK. Go ahead and install all three components and accept the defaults for each one. Doing this means your JDK and JRE will be installed in the default locations for your operating system. On Windows, that's C:\Program Files\Java, as seen in Figure 4.

Matthew Tyson

Figure 4. Java installed

The JDK on the command line Installing the JDK and JRE adds the java command to your command line. You can verify this by dropping into a command shell and typing java -version, which should return the Java version you installed. (In some cases you'll have to restart your system for this change to your system path to fully take.) It's good to have java installed, but what about javac? You'll need this JDK element to compile your Java files. The javac command The javac command lives inside the /jdk directory, but is not automatically added to the system path during installation. We have the option to install javac ourselves, or we could install an IDE that includes this command. We'll start by compiling and running a Java program the old-fashioned way.

A simple Java program Step 1. Write a simple Java program Create a new text file, called Intro.java and place it somewhere on your computer, like your Documents folder. Next, add the code from Listing 1, which is a very simple Java program.

Listing 1. Intro.java

public class Intro {

public static void main(String[] args) { System.out.println("Welcome to the JDK!"); }

}

Step 2. Compile with the JDK Next, use the JDK compiler to turn your text file into an executable program. Compiled code in Java is known as bytecode, and carries the .class extension. You'll use the javac command, which stands for Java compiler. Type the full path to the command into your command shell, and pass the Intro.java file as a command. On my system, that looks like Listing 2. Listing 2. Compile with the JDK

"C:\Program Files\Java\jdk-10.0.1\bin\javac.exe" Intro.java

That should result in a successful compile. The javac will not respond with a success message; it will just output the new file. Any errors will result in console output. Step 3. Run the .class file You should now see the Intro.class file in the same directory as Intro.java. You can run it by typing: java Intro, which will result in Listing 3. Note that you don't include the .class when typing this command. Listing 3. Running Intro.class

C:\Users\mtyson\Documents>java Intro Welcome to the JDK!

The jar command The javac is the star of the JDK, but the /bin directory contains other tools you will need. Probably the most prominent after javac is the jar tool.

A .jar file is a packaged set of Java classes. Once the compiler has created the .class files, the developer can put them together in a .jar, which compresses and structures them in a predictable fashion. Let's convert Intro.class to a jar file. Navigate back to the directory where you placed your Intro.java, and type the command you see in Listing 4. Listing 4. Create a JAR file

C:\Users\mtyson\Documents>"c:\Program Files\Java\jdk-10.0.1\bin\jar.exe" -create --file intro.jar Intro.class

Executing the jar Now you'll see an intro.jar file in the directory. You can make use of the .jar by adding it to your classpath and executing the program inside, as shown here:

java -cp intro.jar Intro

The -cp switch tells Java to add the jar to the classpath. A .jar file is overkill for this tiny program, but they're indispensable as programs grow in size and rely on third-party packages.

The JDK in your IDE Looking back to the JDK download page, you may have noticed the option to download the JDK with the Netbeans IDE. An IDE, or integrated development environment, is software that provides a cohesive set of tools for developing applications. Think of an IDE as a visual operating system with a set of tools, like a file browser and text editor, with additional capabilities specific to development, like code completion and formatting. In Java development, one of the key things the IDE does is manage compilation for you. That is, the IDE automatically runs the compile process in the background so you don't have to continually do it yourself. An IDE also provides play-by-play feedback as you go, catching coding errors on the fly. Several solid IDEs exist for Java. You've seen how the JDK works on the command-line, so now Let's take a quick look at how it works in the Eclipse IDE.

Eclipse and the JDK Installing Eclipse is outside the scope of this guide, but it's a simple process. Eclipse includes an installer like any other program, and you can find the right installer for your operating system here. With Eclipse installed, open the Window menu item from the menu bar and select preferences. Inside the preferences window, you'll see the Java item. Open it, and inside you'll see the Compiler item. Clicking that will reveal some options for the JDK. Figure 5 shows a screenshot of the JDK options in Eclipse.

Matthew Tyson

Figure 5. Eclipse JDK options As previously mentioned, you will need to select the correct JDK version for your project. Under the hood, the IDE will run the JDK compiler, just as you ran it from the command line. The Eclipse IDE also has its own JDK instance. The IDE manages the JDK and JRE for you, which makes life much easier!

Conclusion This article is the second in a short series introducing three core Java platform components: the JVM, JDK, and JRE. Look for the next article in the series, where you'll learn all about the Java Runtime Environment.

What is the JRE? Introduction to the Java Runtime Environment The JRE creates the JVM and ensures dependencies are available to your Java programs 











By Matthew Tyson JavaWorld | SEPTEMBER 11, 2018 01:08 PM PT

IDG / Oracle / Vasabii / Getty Images MORE LIKE THIS



What is the JDK? Introduction to the Java Development Kit



What is the JVM? Introducing the Java Virtual Machine



JVM, JDK, JRE: What's the difference? RELATED ARTICLES



20 practical Python libraries for every Python programmer



CI/CD your way: 11 on-prem options for continuous...



Review: Google Cloud AutoML is truly automated machine... See all Insider Together, the Java Development Kit (JDK), the Java Virtual Machine (JVM), and the Java Runtime Environment (JRE) form a powerful trifecta of Java platform components for

developing and running Java applications. I've previously introduced the JDK and JVM. In this quick tutorial, you'll learn about the JRE, which is the runtime environment for Java. Practically speaking, a runtime environment is a piece of software that is designed to run other software. As the runtime environment for Java, the JRE contains the Java class libraries, the Java class loader, and the Java Virtual Machine. In this system:   

The class loader is responsible for correctly loading classes and connecting them with the core Java class libraries. The JVM is responsible for ensuring Java applications have the resources they need to run and perform well in your device or cloud environment. The JRE is mainly a container for those other components, and is responsible for orchestrating their activities. We'll dig a lot deeper into how these components work together in the sections that follow. Installing the JDK, JRE, and JVM From an installation perspective, anytime you download a JDK, it will include a versioncompatible JRE, and that JRE will include a default JVM. You also can download the JRE separately from the JDK, and you may choose from a variety of JVMs. Defaults work well for most implementations, especially when you're starting out with Java.

What is a runtime environment? A software program needs to execute, and to do that it needs an environment to run in. The runtime environment loads class files and ensures there is access to memory and other system resources to run them. In the past, most software used the operating system (OS) as its runtime environment. The program ran inside whatever computer it was on, but relied on operating system settings for resource access. Resources in this case would be things like memory and program files and dependencies. The Java Runtime Environment changed all that, at least for Java programs. WORA for Java When it was first introduced, Java's "write once, run anywhere" principle was considered revolutionary, but today it's been adopted as a norm for most software systems.

The Java Runtime Environment We can look at software as a series of layers that sit on top of the system hardware. Each layer provides services that will be used (and required) by the layers above it. The Java Runtime Environment is a software layer that runs on top of a computer's operating system, providing additional services specific to Java.

The JRE smoothes over the diversity of operating systems, ensuring that Java programs can run on virtually any OS without modification. It also provides value-added services. Automatic memory management is one of the JRE's most important services, ensuring that programmers don't have to manually control the allocation and reallocation of memory. In short, the JRE is a sort of meta-OS for Java programs. It's a classic example of abstraction, abstracting the underlying operating system into a consistent platform for running Java applications. [ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12part course! ] How the JRE works with the JVM A Java Virtual Machine is a running software system responsible for executing live Java programs. The JRE is the on-disk system that takes your Java code, combines it with the necessary libraries, and starts the JVM to execute it. The JRE contains libraries and software that your Java programs need to run. As an example, the Java class loader is part of the Java Runtime Environment. This important piece of software loads compiled Java code into memory and connects the code to the appropriate Java class libraries. In the layered view I just described, the JVM is created by the JRE. From a package perspective, the JRE contains the JVM, as Figure 1 shows.

Matthew Tyson

Figure 1. The JRE contains the JVM

Installing and using the JRE While there is a conceptual side to the JRE, in real-world practice it's just software installed on a computer, whose purpose is to run your Java programs. As a developer, you'll mostly work with the JDK and JVM, because those are the platform components you use to develop and run

your Java programs. As a Java application user, you would be more involved with the JRE, which lets you run those programs. In most cases, your computer will come with Java installed, and the JRE will be included with that. If you do ever need to manually install or upgrade, you can download the current JRE version from Oracle. JRE versions The Java Runtime Environment is updated for each new version of Java, and its version numbers align with the Java platform versioning system, so for example JRE 1.8 runs Java 8. While you have a variety of JDK packages to choose from (such as Enterprise Edition or Standard Edition) that isn't the case with the JRE. Most computers run a JRE developed for Java SE, which is able to run any Java application regardless of how it was developed. Most mobile devices come with a JRE for Java ME, which is pre-installed on the mobile device and is not available for download. Once the JRE is installed, you can interact with it on the command-line by entering java version, which will tell you what version is installed. On POSIX systems, you can always check the installed location with which java.

The JRE in devops The JRE is not very noticeable in the development stage, where it mostly just runs your programs in the OS or IDE of your choice. The JRE plays a slightly more prominent role devops and systems administration because it's used for monitoring and configuration. Basically, the JRE provides the "knobs" you would use to configure and control the characteristics of a Java application. Memory usage is a prime example, the bread and butter of systems administration. While memory usage is always important, it's vital in cloud configurations, and devops is a cloud-based technology. If you're working in a devops environment, or interested in branching out into devops, it's a good idea to understand how Java memory works and how it's monitored in the JRE. Devops or sysadmin? Devops is a new term, but it describes something that's been true for decades, which is the interoperability between development and operations. In this sense, devops is just a newer term for what used to be called operations or systems administration. Like sysadmin, an important aspect of devops is administering the systems necessary to execute software. Managing the JRE is a part of managing systems that run Java applications.

Java memory and the JRE Java memory consists of three components: the heap, stack and metaspace (which was previously called permgen).

  

Metaspace is where Java keeps your program's unchanging info like class definitions. Heap space is where Java keeps variable content. Stack space is where Java stores function execution and variable references. Memory management in Java 8 Until Java 8, metaspace was known as permgen. Besides being a much cooler name, metaspace is a significant change to how developers interact with Java's memory space. Previously, you would use the command java -XX:MaxPermSize to monitor the size of permgen space. From Java 8 forward, Java automatically increases the size of the metaspace to accomodate your program's meta-needs. Java 8 also introduced a new flag, MaxMetaspaceSize, which can be used to limit the metaspace size. The other memory options, heap and stack, remain the same in Java 8. Configuring heap space Heap space is the most dynamic part of the Java memory system. You can use the -Xms and Xmx flags to tell Java how big to start the heap, and how big to allow it to become. Understanding how to tune these flags for specific program needs is an important aspect of memory management in Java. The ideal is to make the heap big enough to attain the most efficient garbage collection. That is, you want to allow enough memory to let the program run, but you do not want it to be any bigger than necessary. Configuring stack space Stack space is where function calls and variable references are queued. Stack space is the source of the second-most-notorious error in Java programming: the stack overflow exception (the first is the null pointer exception). The stack overflow exception indicates that you've run out of stack space because too much of it has been reserved. Usually, you'll get a stack overflow when a method or methods call each other in a circular fashion, thereby devoting an ever-growing number of function calls into the stack. You use the -Xss switch to configure the stack starting size. The stack then grows dynamically according to the program's needs. Java application monitoring Although application monitoring is a function of the JVM, the JRE provides configuration options, which are the necessary baseline for monitoring. A variety of tools are available for monitoring Java applications, from the classics (like the Unix command top) to sophisticated remote monitoring solutions like Oracle's infrastructure monitoring. In between these options are visual profilers like VisualVM that allow for inspecting a running JVM. These tools enable tracking down hotspots and memory leaks, as well as watching overall memory consumption in your system.

Conclusion

The Java Runtime Environment is the on-disk program that loads Java applications for the JVM to execute. A JRE is included by default when you download the Java Development Kit, and each JRE includes the core Java class libraries, a Java class loader, and a Java Virtual Machine. It's helpful to understand how the JVM, JDK and JRE interact, especially for working in cloud and devops environments. In these environments, the JRE takes a stronger role in monitoring and configuration than it would in traditional Java application development.

https://www.javacodegeeks.com/2018/04/jvm-architecture-overview-of-jvm-and-jvmarchitecture.html

JVM Architecture: Overview of JVM and JVM Architecture Posted by: Yatin Batra in Core Java April 6th, 2018 1 Comment 3152 Views

Hello readers! In this tutorial, we will understand and learn the Java Virtual Machine (JVM) and its architecture. This tutorial will help you to correctly answer the below questions:  What is JVM in Java? 

Different components of JVM



Difference between JVM, JRE, and JDK

1. Introduction Java Virtual Machine (JVM) is an abstract virtual machine that resides on your computer and provides a runtime execution environment for the Java bytecode to get executed. JVM is available for many hardware and software platforms but few Java developers know that the Java Runtime Environment (JRE) is the enactment of the Java Virtual Machine (JVM). JVM analyze the bytecode, interprets it, and execute the same bytecode to display the output. The basic function of JVM is to execute the compiled .class files (i.e. the bytecode) and generate an output. Do note, each operating system has a different JVM, but the generated bytecode output is same across all operating systems. This means that the bytecode generated on Windows OS can also run on Linux OS and vice-versa, thus making Java as a platform independent language.

Fig. 1: An overview of Java Virtual Machine

1.1 What JVM does? Java Virtual machine performs the following operations: 



Loading of the required .class and jar files Assigning references and verification of the code



Execution of the code



Provides a runtime environment for the Java bytecode



Garbage collection



Fig. 2: JVM components

1.2 Types of Java Virtual Machines The Java edition has two different implementations of the Java Virtual Machine (JVM) i.e.  Java Hotspot Client VM: This is the default virtual machine of the JDK 2.0 runtime environment and is tuned for best performance while running the applications in a client environment by reducing the application start-up time and memory footprints



Java Hotspot Server VM: This virtual machine is designed for enhanced program execution speed for running the applications in a server environment. This virtual machine is invoked by using the server command line option

1.3 Internal architecture of JVM The diagram shows the key internal components of Java Virtual Machine that conforms to the JVM specification.

Fig. 3: Java Virtual Machine architecture The components that are shown in Fig. 3 are explained below.

1.3.1 Class Loader The class loader subsystem is used for loading/reading the .class files and saving the bytecode in the JVM method area. This subsystem performs three major functions i.e.:  Loading: This component handles the loading of the classes  Linking: This component will verify the generated bytecode and assign references



Initialization: This component will assign the static variables with their original values and execute the static blocks

1.3.2 Runtime Data Areas This subsystem is divided into five major components i.e.  Method Area: This component holds the class level data of each .class file such as metadata, constant runtime pool, static variables, the code for the methods etc. There is only one method area per JVM and is shared among all the classes  Heap Area: This component is a part of JVM memory where all the objects and its corresponding instance variables and arrays are stored. There is only one heap area and is shared across multiple threads as the data stored in this area is not threadsafe  Stack Area: This component is again a part of JVM memory where all the temporary variables are stored. This area has stack frames and allocates one frame for each thread. As the execution of a thread is completed, the corresponding frame also gets destroyed. The stack area is thread-safe as it is not a shared resource and is divided into three sub-entities such as:  Local variable array







Operand Stack



Frame data

This area plays an important role during the method invocation and returns. PC (Program Counter) Registers: This component holds the address of the JVM instruction which is currently executing. Each thread in Java has its own PC register to hold the address of the currently executing instruction Native Method Stacks: This component is written in a different language and holds the native method information. Every thread in Java has a separate native method stack

1.3.3 Execution Engine This component executes the bytecode which is assigned to the runtime data areas and has three major sub-components i.e.:  Interpreter: This component reads the bytecode instructions and executes them in a sequential manner  JIT (Just-in-Time) Compiler: This component counterbalances the Interpreter’s disadvantage of slow execution and improves the performance. JIT compiler compiles the similar part of the bytecode at the same time and thus reduces the total time needed for compilation. The compiler in this component refers to a translator which converts the JVM instruction set to the OS-specific instruction set  Garbage Collection: This component is a part of execution engine which frees up the memory by collecting and removing the unreferenced objects

Fig. 4: Execution Engine in JVM

1.3.4 Native Method Interface (JNI) This component is a programming framework that allows the Java code to call or be called by the libraries and the native applications (i.e. the programs specific to the hardware and the OS of a system).

1.3.5 Native Method Libraries This component is a collection of native C, C++ libraries which are required by the execution engine.

1.4 JVM vs. JRE vs. JDK JDK, JRE, and JVM are the three important keywords of Java programming and many developers’ fails to get the difference between them.  Java Virtual Machine (JVM): JVM is a virtual machine which provides a runtime environment for executing the Java bytecode

Java Runtime Environment (JRE): JRE is an environment within which the JVM runs and has class libraries and other files that Java Virtual Machine uses at the time of execution. In other words, JRE = Java Virtual Machine (JVM) + Libraries to run the application  Java Development Kit (JDK): JDK is the parent set of the JRE and has everything that JRE contains along with the development tools such as a compiler, debugger etc. In other words, JDK = Java Runtime Environment (JRE) + Development tools Here is the pictorial representation of JVM, JRE, and JDK. 

Fig. 5: JVM vs. JRE vs. JDK Representation

1.5 How to Compile and Execute a Java class? This section will demonstrate the compilation and execution of a Java class. Let’s understand this process with the help of sample code snippet.

1.5.1 Creating a Java file

Open the operating system command prompt and we will use the ‘notepad’ to create a simple Java class. The following Java command can be used. > notepad _sample_file_name_with_extension_ 1 The command gives the below output.

Fig. 6: Creating a Java file using Notepad

1.5.2 Writing the sample Java Code As shown in Fig. 6, the command will open a notepad and developers can add the sample code to the Welcome.java file that displays a dummy output. The sample code is shown in Fig. 7 and will display a greetings message on successfulexecution.

Fig. 7: Adding the sample code

1.5.3 Compiling the Java class After saving the code in Welcome.java file, developers will need to compile it. This compilation will produce the Welcome.class file which in turn will generate a .class file. To compile the file, the following Java command can be used. > javac _Java_file_name_with_extension_ 1 The command gives the below output.

Fig. 8: Compiling the file

1.5.4 Executing the Java class Now, developers will need to execute the generated Welcome.class file to display the output. To execute the file, the following Java command can be used. > java _Compiled_file_name_with_extension_ 1 The command gives the below output.

Fig. 9: Executing the file That’s all for this post. Happy Learning!

2. Conclusion In this tutorial, we had an overview of the Java Virtual Machine (JVM) and its core components. This tutorial can be summarized as:  JVM is the virtual machine that executes the Java code and produces output by converting the Java bytecode into the machine language  

JIT compiler is a sub-component of the JVM and is used to speed up the performance and the execution time Java is slow in execution due to:  Dynamic linking 

Run-time conversion of the bytecode to the native machine code

However, these bottleneck performance issues are addressed to an extent in the new Java Development Kit (JDK) versions. Developers can download the sample code snippet in the Downloads section.

3. Download the Source Code This was an overview tutorial of Java Virtual Machine (JVM) and its core components.

Related Documents

Jdk Jre Jvm.docx
May 2020 6
Jdk
April 2020 6
Proyectos Jre
June 2020 2
Proyectos Jre
June 2020 3
Jdk 5 - Annotations
November 2019 4

More Documents from ""

Jdk Jre Jvm.docx
May 2020 6
Antiviral Therapy
December 2019 42
Umi-umd-2415 H5n1
December 2019 29
Deepviewmanual H5n1
December 2019 46
12b-01.pdf
May 2020 38