• No results found

Finding Java Classes with CLASSPATH

In document Java 8 Programming Black Book (Page 66-69)

Importing packages and classes only indicates to the compiler where to look for the code it needs—it does not increase the size of your code. For that reason, the bytecode file App.class will be of the same size regardless of whether you use the import java.util.Date; statement or the import java.util.*; statement.

This is fine if you stick with importing the Sun-provided classes because Java knows where to look for the classes it was installed with. But what if you want to import your own classes or ones provided by a third party?

Here’s an example. Suppose you have a user-defined package named printer, having a class named Printer in a file named Printer.java, and that class has one method named print:

package printer;

public class Printer {

public void print() { System.out.println("Hello from Java!"); } }

You might want to make use of the print method in other classes. In this case, we are creating a new object of the Printer class using the new operator and using that object’s print method in an application named App:

public class App {

public static void main(String[] args) { (new Printer()).print(); } }

To do this, you can import the Printer class as follows (note that you can also place the code for the Printer class in the same file as the App class, in which case you wouldn’t have to import the Printer class):

import printer.*;

public class App {

public static void main(String[] args) { (new Printer()).print(); } }

Here, we have learned how to import a Java built-in and a user-created class in our Java program. To learn about the implementation part and how to actually create and import packages in our classes, refer Chapter 27.

However, suppose you want to store all your classes in a directory named, say, c:\classes. How will the Java compiler find Printer.class there? To answer that question, take a look at the next solution on CLASSPATH.

Finding Java Classes with CLASSPATH

“That Johnson!” the NP says. “He gave me a new Java class file, johnson.class, to work with, and it’s supposed to solve my problems with that spreadsheet. But, Java claims it can’t find johnson.class!” “Where are you keeping that file?” you ask. “In a special directory I made for it,” the NP says, “called darnjohnson.”

“That’s your problem,” you say. “You have to include the darnjohnson directory in your class path.”

By default, Java will be able to find its bootstrap classes (the ones it comes with), extension classes (those that use the Java Extension Framework), and classes in the current directory (that is, where you are compiling your program). Classes can be stored in the .class files, .jar files, and .zip files. Java can search all these types of files.

Bootstrap classes are the classes that come by default with JDK, and Extension classes are the classes that use the Java Extension Framework. But, what if you want to have Java search for classes in another directory or in a .jar file supplied by a third party? You can do that with the CLASSPATH environment variable because Java uses this variable to determine where you want to search for classes.

Immediate Solutions

Here’s an example that was first introduced in the previous solution. Say that you have a class named Printer in a file named Printer.java and that class has one method named print:

public class Printer {

public void print() { System.out.println("Hello from Java!"); } }

In the previous solution, you want to use the print method in another class—as in this case where we are creating a new object of the Printer class by using the new operator and using that object’s print method in an application named App:

import printer.*;

public class App {

public static void main(String[] args) { (new Printer()).print(); } }

This works if Printer.class is in the same directory in which you are compiling this application, because the Java compiler will search the current directory by default. But, suppose you want to store all your classes in a directory named c:\classes. How will the Java compiler find Printer.class there?

To make the Java compiler search c:\classes, you can set the CLASSPATH environment variable to include that directory. By default, there are no paths or directories in CLASSPATH, but you can add a semicolon-separated list to CLASSPATH, like this (note that it’s important here not to have any spaces around the equals sign):

SET CLASSPATH=c:\classes;c:\newclasses

You can also determine the current CLASSPATH setting by typing the SET CLASSPATH at the command line and pressing Enter. To permanently set the classpath in Windows 7, follow these steps:

1. Click the Windows icon on the taskbar, right-click the Computer, and select the Properties option. You can also reach the same location by opening the Control Panel| System and Security| System.

2. Click the Advanced system settings option in the System Properties window. This opens up the System Properties dialog box.

3. Click the Environment Variables button to open the Environment Variables dialog box.

4. Edit the CLASSPATH setting the way you want.

5. You may append the new classpath to the already existing CLASSPATH variable or create a new CLASSPATH variable.

6. Finally, click OK.

Note that if you are appending the CLASSPATH variable, the different paths must be separated by semicolons (;).

Now, the Java compiler (and other Java tools, such as the java tool) will know enough to search c:\classes and c:\newclasses automatically. This means that the following code will now work if Printer.class is in c:\classes because that directory is in CLASSPATH:

import printer.*;

public class App {

public static void main(String[] args) { (new Printer()).print(); } }

You can append the current settings in CLASSPATH to a new setting, like this:

SET CLASSPATH=c:\classes;c:\newclasses;%CLASSPATH%

Note that you can also search .jar and .zip files for classes, as shown here:

SET CLASSPATH=server.jar;classes.zip;%CLASSPATH%

Originally, CLASSPATH was a big headache for beginners in Java programming because no classes were considered bootstrap classes, which meant that you had to set up and understand CLASSPATH before you could use Java at all. That’s been fixed with the concept of bootstrap classes, which are the classes that come with Java (and are searched automatically). However, if you want to use non-standard packages or store your own classes in other directories, it’s important to know how to set CLASSPATH.

Summary

In this chapter, you got familiar with Java 8, its environment, and designing of programs. The chapter introduced you to Swings and Utilities. In addition, it discussed the enhancements in JDBC and provided a brief description of garbage collector. You also learned how to install Java. Toward the end, you developed simple Java programs with the introduction of how to import the packages.

In the next chapter, you will learn about the variables, arrays, and strings.

Chapter 2: Variables, Arrays, and Strings

2

Variables, Arrays,

In document Java 8 Programming Black Book (Page 66-69)

Related documents