Compiling, Running, and Distributing FourOfAKind
A. SIX_OF_CLUBS B. TWO_OF_SPADES
C. SIX_OF_HEARTS D. SIX_OF_SPADES
Which card do you want to throw away (A, B, C, D)? B
Human wins!
Human's cards:
SIX_OF_CLUBS SIX_OF_DIAMONDS SIX_OF_HEARTS
SIX_OF_SPADES Computer's cards:
SEVEN_OF_HEARTS TEN_OF_HEARTS SEVEN_OF_CLUBS SEVEN_OF_DIAMONDS
Although Four of a Kind is not much of a card game, you might decide to share the FourOfAKind application with a friend. However, if you forget to include even one of the application’s five supporting classfiles, your friend will not be able to run the application.
You can overcome this problem by bundling FourOfAKind’s six classfiles into a single JAR (Java ARchive) file, which is a ZIP file that contains a special directory and the .jar file extension. You can then distribute this single JAR file to your friend.
The JDK provides the jar tool for working with JAR files. To bundle all six classfiles into a JAR file named FourOfAKind.jar, you could specify the following command line, where c tells jar to create a JAR file and f identifies the JAR file’s name:
jar cf FourOfAKind.jar *.class
After creating the JAR file, try to run the application via the following command line:
java -jar FourOfAKind.jar
Instead of the application running, you will receive an error message having to do with java not knowing which of the JAR file’s six classfiles is the main classfile (the file whose class’s main() method executes first).
You can provide this knowledge via a text file that is merged into the JAR file’s manifest, a special file named MANIFEST.MF that stores information about the contents of a JAR file, and which is stored in the JAR file’s META-INF directory. Consider Listing 1–9.
Listing 1–9. Identifying the application’s main class Main-Class: FourOfAKind
Listing 1–9 tells java which of the JAR’s classfiles is the main class file. (You must leave a blank line after Main-Class: FourOfAKind.)
The following command line, which creates FourOfAKind.jar, includes m and the name of the text file providing manifest content:
jar cfm FourOfAKind.jar manifest *.class
This time, java -jar FourOfAKind.jar succeeds and the application runs because java is able to identify FourOfAKind as the main classfile.
EXERCISES
The following exercises are designed to test your understanding of what Java means, the JDK, NetBeans, Eclipse, and Java application development:
1. What is Java?
2. What is a virtual machine?
3. What is the purpose of the Java compiler?
4. True or false: A classfile’s instructions are commonly referred to as bytecode.
5. What does the virtual machine’s interpreter do when it learns that a sequence of bytecode instructions is being executed repeatedly?
6. How does the Java platform promote portability?
7. How does the Java platform promote security?
8. True or false: Java SE is the Java platform for developing servlets.
9. What is the JRE?
10. What is the difference between the public and private JREs?
11. What is the JDK?
12. Which JDK tool is used to compile Java source code?
13. Which JDK tool is used to run Java applications?
14. What is the purpose of the JDK’s jar tool?
15. What is Standard I/O?
16. What is an IDE?
17. Identify two popular IDEs.
18. What is pseudocode?
19. How would you list FourOfAKind.jar’s table of contents (list of directories and files contained in the JAR file)?
20. Modify FourOfAKind to give each player the option of picking up the top card from the deck or discard pile (assuming that the discard pile is not empty; it is empty when the human player goes first and starts his/her first turn). Display the discard pile’s top card for the human player’s benefit. (Do not display the deck’s top card.)
Summary
Java is a language and a platform. The language is partly patterned after the C and C++
languages to shorten the learning curve for C/C++ developers. The platform consists of a virtual machine and associated execution environment.
Developers use different editions of the Java platform to create Java programs that run on desktop computers, web browsers, web servers, mobile information devices, and embedded devices. These editions are known as Java SE, Java EE, and Java ME.
Developers also use a special Google-created edition of the Java platform to create Android apps that run on Android-enabled devices. This edition, known as the Android platform, largely consists of Java core libraries and a virtual machine known as Dalvik.
The public JRE implements the Java SE platform and makes it possible to run Java programs. The JDK provides tools (including the Java compiler) for developing Java programs and also includes a private copy of the JRE.
Working with the JDK’s tools at the command line is not recommended for large projects, which are hard to manage without the help of an integrated development environment. Two popular IDEs are NetBeans and Eclipse.
Application development is not an easy task. All applications except for the most trivial require careful planning or you will probably waste your (and your users’) time and money. One way to develop applications efficiently involves using pseudocode.
FourOfAKind gave you a significant taste of the Java language. Although much of its source code is probably hard to understand right now, you will find it much easier to grasp after reading Chapter 2, which introduces you to Java’s language fundamentals.
43