Java Programing

June 19, 2007

If you don’t specify the CLASSPATH environment variable,

Filed under: Java Programming — webmaster @ 5:23 am

If you don’t specify the CLASSPATH environment variable, it defaults to the current directory (.); this means that the files in your current directory are always available. If you change the class path and don’t include the current directory, these files will no longer be accessible. 3.4 The Java Compiler In this section, we’ll say a few words about javac, the Java compiler that is supplied as part of Sun’s SDK. (If you are happily working in another development environment, you may want to skip ahead to the next section.) The javac compiler is written entirely in Java, so it’s available for any platform that supports the Java runtime system. The ability to support its own development environments is an important stage in a language’s development. Java makes this bootstrapping automatic by supplying a ready-to-run compiler at the same cost as porting the interpreter. javac turns Java source code into a compiled class that contains Java virtual machine byte-code. By convention, source files are named with a .java extension; the resulting class files have a .class extension. Each source code file is a single compilation unit. As you’ll see in Chapter 6, classes in a given compilation unit share certain features, such as package and import statements. javac allows you one public class per file and insists the file have the same name as the class. If the filename and class name don’t match, javac issues a compilation error. A single file can contain multiple classes, as long as only one of the classes is public. Avoid packing many classes into a single source file. Including non-public classes in a .java file is one easy way to tightly couple such classes to a public class. But you might also consider using inner classes (see Chapter 6). Now for an example. Place the following source code in file BigBird.java: package animals.birds; public class BigBird extends Bird { … } Then compile it with: % javac BigBird.java Unlike the Java interpreter, which takes just a class name as its argument, javac needs a filename to process. The previous command produces the class file BigBird.class in the same directory as the source file. While it’s useful to have the class file in the same directory as the source for testing a simple example, for most real applications you’ll need to store the class file in an appropriate place in the class path. You can use the -d option to javac to specify an alternative directory for storing the class files it generates. The specified directory is used as the root of the class hierarchy, so .class files are placed in this directory or in a subdirectory below it, depending on whether the class is contained in a package. (The compiler creates intermediate subdirectories automatically, if necessary.) For example, we can use the following command to create the BigBird.class file at /home/vicky/Java/classes/animals/birds/BigBird.class: % javac -d /home/vicky/Java/classes BigBird.java You can specify multiple .java files in a single javac command; the compiler creates a class file for each source file. But you don’t need to list source files for other classes that your class references, as - 60

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Java Web Hosting