分享

What is the JDK? Introduction to the Java Developm...

 imnobody2001 2019-05-17

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.

jw whatisjdk fig1 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.

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).

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.

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.

jw whatisjdk fig2 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!

Go ahead and download the Java Standard Edition JDK. Options for that are shown in Figure 3.

jw whatisjdk fig3 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.

jw whatisjdk fig4 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 IntroWelcome 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.

jw whatisjdk fig5 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.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多