DOC PREVIEW
Berkeley COMPSCI 61B - Compiling Java Programs

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61B P. N. HilfingerBasic Compilation: javac, gcc, g++1 Compiling Java Programs[The discussion in this section applies to Java 1.2 tools from Sun Microsystems. Tools from other manufac-turers and earlier tools from Sun differ in various details.]Programming languages do not exist in a vacuum; any actual programming done in any language onedoes within a programming environment that comprises the various programs, libraries, editors, debuggers,and other tools needed to actually convert program text into action.The Scheme environment that you used in CS61A was particularly simple. It provided a componentcalled the reader, which read in Scheme-program text from files or command lines and converted it intointernal Scheme data structures. Then a component called the interpreter operated on these translated pro-grams or statements, performing the actions they denoted. You probably weren’t much aware of the reader;it doesn’t amount to much because of Scheme’s very simple syntax.Java’s more complex syntax and its static type structure (as discussed in lecture) require that you be abit more aware of the reader—or compiler, as it is called in the context of Java and most other “production”programming languages. The Java compiler supplied by Sun Microsystems is a program called javac onour systems. You first prepare programs in files (called source files) using any appropriate text editor (Emacs,for example), giving them names that end in ‘.java’. Next you compile them with the java compiler tocreate new, translated files, called class files, one for each class, with names ending in ‘.class’. Onceprograms are translated into class files, there is a variety of tools for actually executing them, includingSun’s java interpreter (called ‘java’ on our systems), and interpreters built into products such as Netscapeor Internet Explorer. The same class file format works (or is supposed to) on all of these.In the simplest case, if the class containing your main program or applet is called, then you shouldstore it in a file called.java, and you can compile it with the commandjavac.javaThis will produce .class files forand for any other classes that had to be compiled because they werementioned (directly or indirectly) in class. For homework problems, this is often all you need to know,and you can stop reading. However, things rapidly get complicated when a program consists of multipleclasses, especially when they occur in multiple packages. In this document, we’ll try to deal with the morestraightforward of these complications.1.1 Where ‘java’ and ‘javac’ find classesEvery Java class resides in a package (a collection of classes and subpackages). For example, the standardclass String is actually java.io.String: the class named String that resides in the subpackagenamed io that resides in the outer-level package named java. You use a package declaration at thebeginning of a .java source file to indicate what package it is supposed to be in. In the absence of such adeclaration, the classes produced from the source file go into the anonymous package, which you can thinkof as holding all the outer-level packages (such as java).The Java interpreter. When the java program (the interpreter) runs the main procedure in a class, andthat main procedure uses some other classes, let’s say A and p.B, the interpreter looks for files A.classand B.class in places that are dictated by things called class paths. Essentially, a class path is a listof directories and archives (see1.4 below for information on archives). If the interpreter’s class path1contains, let’s say, the directories and, then upon encountering a mention of class A, java will lookfor a file named /A.class or/A.class. Upon encountering a mention of p.B, it will look for /p/B.class or/p/B.class.The class path is cobbled together from several sources. All Sun’s java tools automatically supply abootstrap class path, containing the standard libraries and such stuff. If you take no other steps, the onlyother item on the class path will be the directory ‘.’ (the current directory). Otherwise, if the environmentvariable CLASSPATH is set, it gets added to the bootstrap class path. Our standard class setup has ‘.’ andthe directory containing the ucb package (with our own special classes, lovingly concocted just for you). Ifyou print its value with, for example,echo $CLASSPATHyou’ll see something like.:/home/ff/cs61b/lib/java/classes(the colon is used in place of comma (for some reason) to separate directory names). It is also possible toset the class path (overriding the CLASSPATH environment variable) for a single program execution withjava -classpath PATH ...but I really don’t recommend this.The Java compiler. The compiler looks in the same places for .class files, but its life is more compli-cated, because it also has to find source files. By default, when it needs to find the definition of a class, itlooks for file.java in the same directories it looks for.class. This is the easiest case to deal with.If it does not find.class, or if it does find.class but notices that it is older (less recently modified)than the corresponding source file.java, it will automatically (re)compile.java. To use this defaultbehavior, simply make sure that the current directory (‘.’) is in your class path (as it is in our default setup)and put the source for a class A (in the anonymous package) in A.java in the current directory, or for aclass p.B in p/B.java, etc., using the commandsjavac A.javajavac p/A.javarespectively, to compile them.It is also possible to put source files, input class files, and output class files (i.e., those created bythe compiler) in three different directories, if you really want to (I don’t think we’ll need this). See the-sourcepath and -d options in the on-line documentation for javac, if you are curious.1.2 Multiple classes in one source fileIn general, you should try to put a class namedin a file named.java (in the appropriate directory). Forone thing, this makes it possible for the compiler to find the class’s definition. On the other hand, althoughpublic classes must go into files named in this way, other classes don’t really need to. If you have a non-public class that really is used only by class, then you can put it, too, into.java. The compiler willstill generate a separate


View Full Document

Berkeley COMPSCI 61B - Compiling Java Programs

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Compiling Java Programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Compiling Java Programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Compiling Java Programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?