7.1-7.2-7.5 Using javac/java Commands & JAR filesHomepage  « Java6 Certification « 7.1-7.2-7.5 Using javac/java Commands & JAR files

We start this lesson by looking at the javac command we use to compile our Java programs and the java command to run our programs. We finish the lesson with a look at JAR files and how to use them.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 7: Fundamentals

    • Given a code example and a scenario, write code that uses the appropriate access modifiers, package declarations, and import statements to interact with (through access or inheritance) the code in the example.

    • Given an example of a class and a command-line, determine the expected runtime behavior.

    • Given the fully-qualified name of a class that is deployed inside and/or outside a JAR file, construct the appropriate directory structure for that class. Given a code example and a classpath, determine whether the classpath will allow the code to compile successfully.

Access Modifiersgo to top of page Top

The table below shows the four types of access available in Java from the most open (public) to the most restrictive (private). We can only explicitly apply the public access modifier to our top-level classes (the classes we compile) but for members and constructors we can explicitly apply the protected and private access modifiers as well. We will talk about packaging in the Packages lesson, but for now we are going to examine how to protect our class members from unwanted access and modification.

Access modifier Class Member Constructor Description
publicYesYesYesA top-level class may be declared with the public access modifier, and if it is the class is accessible to all other classes everywhere.
A member may be declared with the public access modifier, and if it is the member is accessible to all other classes everywhere, assuming the class it resides in is accessible.
A constructor may be declared with the public access modifier, and if it is the constructor is accessible to all other classes everywhere, assuming the class it resides in is accessible.
protectedNoYesYesA member may be declared with the protected access modifier, and if so, is only accessible within its own package and also by a subclass of its class in other packages.
A constructor may be declared with the protected access modifier, and if so, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
See the Inheritance Basics lesson for more information on subclassing.
no modifier
package-private /
(the default)
YesYesYesIf a top-level class has no explicit access modifier, which is the default and is also known as package-private, it is accessible only within its own package.
If a member has no explicit access modifier it is only accessible within its own package.
If a constructor has no explicit access modifier, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
privateNoYesYesA member may be declared with the private access modifier, and if it is the member is only accessible within its own class.
A constructor may be declared with the private access modifier, and if it is the constructor can only be constructed from within its own class.

See the OO Concepts - Encapsulation lesson for example usage and information on how we use getters and setters to enforce tight encapsulation.

Packagesgo to top of page Top

In Java we can keep libraries of related classes together using a package and this is in essence what the Java language is, a collection of packaged libraries.

  • If a class is part of a package then the package statement must be the first line of code in a source file.

There are two major benefits to the package approach adopted by Java.

  • A package provides the apparatus for us to group related classes together under the same umbrella.
    1. We need a way to uniquely identify classes and Java doesn't allow us to have two top-level classes with the same name, within the same namespace. By using packaging we can partition the namespace to alleviate naming conflicts.
    2. Within any reasonable sized system we will have classes that relate to different aspects of said system, such as the Model View Controller paradigm. Packaging allows us to separate concerns into areas such as a Model subpackage, a View subpackage and so on. This makes the whole development process easier and more manageable.
  • A package is part of the Java mechanism we use to enforce Encapsulation.
    1. By not marking members within a package with an access modifier, we are saying that these members are package-private and can only be accessed from within this package.
    2. By marking members with the protected access modifier, we are saying that these members can only be accessed from within this package or from a subclass outside the package.

The following table shows usage of a single package statement and an example of a multiple hierarchy package statement.

Package Form Example Description
Single package
package pkg;package A;This file is part of package A.
Multiple package hierarchy
package pkg.subPkg1.subPkg2...subPkgN;package A.B.C;This file is part of Package C, which is a subpackage of B, which is a subpackage of A.

Importsgo to top of page Top

Java allows us to import parts of a package, or even the whole package if required for use in our code and we do this using the import keyword.

  • If a class uses the import keyword to import parts or all of a package, then the import keyword must follow the package statement if there is one, or must be the first line of code in a source file if there is no package statement.

The following table shows how to import a single class from a package and all classes from a package using the import statement.

Import Form Example Description
Single class import
import pkg.class;import java.rmi.Remote;Import the Remote class from java.rmi
Multiple class import
import pkg.subPkg1.subPkg2...subPkgN;import java.rmi.*;Import all java.rmi classes

Static Imports

With the introduction of Java6 a new feature became available when using the import statement which is commonly known as static imports. When using the import keyword followed by static we can import all the static members of a class or interface.

The following table shows how to import a single static member from a package and all static members from a package using the import static statement.

Import Static Form Example Description
Single static member import
import static pkg.staticMember;import static java.lang.Math.acos;Import the acos static member from java.lang.Math
Multiple static member import
import static pkg.allStaticMembers;import static java.lang.Math.*;Import all java.lang.Math static members

The javac Commandgo to top of page Top

We have been using the javac command throughout the lessons to compile our Java programs and the java command to run our programs. Now it's time to take a closer look at compiling our Java programs and some of the options available when doing this. Firstly lets take a look at a screenshot of the options available with the javac command:

javac Options

As you can see from the screenshot the javac command comes with a hefty list of options. Luckily for us we only need to know about the -classpath and -d options for certification purposes.

Compiling Using the -d Optiongo to top of page Top

When we have used the javac command to compile our programs we have been putting the compiled bytecode (the *.class) files into the same directory as our source (the *.java) files. Whilst this is fine for the trivial programs we use on the site to aid with learning; when we start creating projects of any size we want to keep source and compiled files in separate directories. There are good reasons for doing this including the testing and deployment of our projects as well as maintainability and version control and to do this we compile using the -d option. To see the -d option in action we will create a simple program and put it into a file structure we have created:


class A { }

The following screenshot shows a simple directory structure we have setup and placed the simple A class in:

javad File Structure

As you can see the class directory is empty and the source directory holds the A class source file. To show how the -d javac compiler option we will compile the A class:

javad Options

In the screenshot above we have compiled the simple program A with the -d javac compiler option as follows:


javac -d classes source/A.java

The -d option is followed by the directory where we want to put our *.class files and this directory must already exist or we get a compiler error. After this we set a relative url path to the source file to be compiled, from the current directory. As you can see from the screenshot above we now have compiled bytecode in the classes directory.

Lets see how we can use a package within our programs and how this affects the compile using the -d option. Here we make a simple B class which is part of a package:


package info.java6tutor;
class B { }

The following screenshot shows a simple directory structure corresponding to the package we have setup and placed the simple B class in:

javad File Structure2

As you can see the source directory has info and java6tutor subdirectories and the classes directory just hold the A class file we compiled earlier. So lets compile the B class using the -d option as follows:


javac -d classes source/info/java6tutor/B.java

javad Options Package

In the screenshot above we have compiled the Bclass with the -d javac compiler option and this has worked, and also created the info and java6tutor subdirectories within the classes directory for us.

The java Commandgo to top of page Top

We have been using the java command throughout the lessons to run our Java programs and saw some of the options available when running our programs when we looked at assertions Flow Control - Using Assertions. Now it's time to take a closer look at running our Java programs and some of the options available when doing this. Firstly lets take a look at a screenshot of the options available with the java command:

java Options

As you can see from the screenshot the java command comes with a hefty list of options. Luckily for us we only need to know about the -classpath options for certification purposes.

Running Using the -classpath Optiongo to top of page Top

We can run programs from anywhere on the file system using the -classpath option, so lets see an example of this. From the c:\_Fundamentals file I am in on Windows I want to run the CastTest1.class file that is stored in the file system on my computer in directory c:\_Collections. Here is the command to do it:


java -classpath ..\_Collections; CastTest1

javaclass Options

The screenshot above shows the results of running the c:\_Collections\CastTest1.class from the current directory, it works just fine. The ..\_Collections; can be split up as follows:

  1. ..\ Go up one directory (use a forward slash in Linux)
  2. _Collections look in specified directory
  3. ; delimit this command (use a colon in Linux)

When you use the -classpath option with the java command, you can also search in the current directory for a class file by putting a . (period) at the start of your search pattern and before any delimiter as follows:


java -classpath .;..\_Collections; CastTest1

  1. . search for class file in current directory
  2. ; delimit this command (use a colon in Linux)
  3. ..\ Go up one directory (use a forward slash in Linux)
  4. _Collections look in specified directory
  5. ; delimit this command (use a colon in Linux)
javaclass Options

The screenshot above shows the results of running the CastTest1.class and also searching the current directory first. It should be noted that we have put copies of the CastTest.class file and the A and B class files it runs, in the root directory of Windows with a different message to show how this works. Before we leave the -classpath option, there is also a shorthand version of this command -cp which you may see used in the certification questions. So whenever you see the -classpath option you can use -cp and vice versa.

See HTML Doctor - Lesson 5 - More About Links for an explanation of absolute and relative paths.

Command Line Argumentsgo to top of page Top

Every time we use the main() method we are including the String[] args parameter in its scope. Lets see how to use this parameter within a method with a code example:


/*
  Command Line Arguments
*/ 
public class UsingACommandLineArg {

    public static void main (String[] args) {
        System.out.println("Have a nice day, " + args[0]);
    }

}

The above program was saved, compiled and run using the command java UsingAParameter Kevin

run cl arg

As you can see from the screenshot above we run UsingACommandLineArg twice. The first time it falls over because we are tring to access an array index that doesn't exist. This of course could be avoided by checking for an index entry first. In the second run we pass a parameter and print out a message. What we are doing here is extracting the first argument from the String array, arrays are zero-index based. We then concatenate it to the end of a string to display on the console.

JAR Filesgo to top of page Top

We can bundle up applications we write for easy distribution and installation using WAR and JAR files. WAR is an acronym for Web Archive, is not part of the certification and is beyond the scope of these tutorials. JAR is an acronym for Java Archive and is part of the certification and will be discussed here.

jar Options

As you can see from the screenshot the jar command gives us quite a few options for archiving our applications and classes into a JAR file.

jar Creation

In the screenshot above we create a JAR named Testjar.jar using some of the JAR creation options available.

Related Java6 Tutorials

Beginning Java6 - Getting Started
Objects & Classes - Enumerations
API Contents - Packages
API Contents - Packages - Imports
API Contents - Packages - Static Imports
Flow Control - Using Assertions

go to home page Java6 Tutor Homepage go to home page Top