DOC PREVIEW
Berkeley COMPSCI 61B - Basic Compilation

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 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 16 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 16 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 16 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 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Compiling Java ProgramsWhere `java' and `javac' find classesMultiple classes in one source fileCompiling multiple filesArchive filesCompiling C and C++Running the compilerLibrariesOptionsThe make utilityBasic Operation and SyntaxVariablesImplicit rulesSpecial actionsDetails of actionsCreating makefilesMakefiles with JavaUNIVERSITY OF CALIFORNIADepartment of Electrical Engineeringand Computer SciencesComputer Science DivisionCS61B P. N. HilfingerFall 1999Basic Compilation: javac, gcc, g++, and gmake1 Compiling Java Programs[The discussion in this section applies to Java 1.2 tools from Sun Microsystems. Tools fromother manufacturers and earlier tools from Sun differ in various details.]Programming languages do not exist in a vacuum; any actual programming done in anylanguage one does within a programming environment that comprises the various programs,libraries, editors, debuggers, and other tools needed to actually convert program text intoaction.The Scheme environment that you used in CS61A was particularly simple. It provideda component called the reader, which read in Scheme-program text from files or commandlines and converted it into internal Scheme data structures. Then a component called theinterpreter operated on these translated programs or statements, performing the actions theydenoted. You probably weren’t much aware of the reader; it doesn’t amount to much becauseof Scheme’s very simple syntax.Java’s more complex syntax and its static type structure (as discussed in lecture) requirethat you be a bit more aware of the reader—or compiler, as it is called in the context ofJava and most other “production” programming languages. The Java compiler supplied bySun Microsystems is a program called javac on our systems. You first prepare programsin files (called source files) using any appropriate text editor (Emacs, for example), givingthem names that end in ‘.java’. Next you compile them with the java compiler to createnew, translated files, called class files, one for each class, with names ending in ‘.class’.Once programs are translated into class files, there is a variety of tools for actually executingthem, including Sun’s java interpreter (called ‘java’ on our systems), and interpreters builtinto products such as Netscape or Internet Explorer. The same class file format works (or issupposed to) on all of these.In the simplest case, if the class containing your main program or applet is called C, thenyou should store it in a file called C.java, and you can compile it with the commandjavac C.java2526 P. N. HilfingerThis will produce .class files for C and for any other classes that had to be compiled becausethey were mentioned (directly or indirectly) in class C. For homework problems, this is oftenall you need to know, and you can stop reading. However, things rapidly get complicatedwhen a program consists of multiple classes, especially when they occur in multiple packages.In this document, we’ll try to deal with the more straightforward 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 standard class String is actually java.io.String: the class named String that residesin the subpackage named io that resides in the outer-level package named java. You use apackage declaration at the beginning of a .java source file to indicate what package it issupposed to be in. In the absence of such a declaration, the classes produced from the sourcefile go into the anonymous package, which you can think of as holding all the outer-levelpackages (such as java).The Java interpreter. When the java program (the interpreter) runs the main procedurein a class, and that main procedure uses some other classes, let’s say A and p.B, the interpreterlooks for files A.class and B.class in places that are dictated by things called class paths.Essentially, a class path is a list of directories and archives (see §1.4 below for informationon archives). If the interpreter’s class path contains, let’s say, the directories D1and D2,then upon encountering a mention of class A, java will look for a file named D1/A.classor D2/A.class. Upon encountering a mention of p.B, it will look for D1/p/B.class orD2/p/B.class.The class path is cobbled together from several sources. All Sun’s java tools automaticallysupply a bootstrap class path, containing the standard libraries and such stuff. If you takeno other steps, the only other item on the class path will be the directory ‘.’ (the currentdirectory). Otherwise, if the environment variable CLASSPATH is set, it gets added to thebootstrap class path. Our standard class setup has ‘.’ and the directory containing the ucbpackage (with our own special classes, lovingly concocted just for you). If you print its valuewith, 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 isalso possible to set the class path (overriding the CLASSPATH environment variable) for asingle program execution withjava -classpath PATH ...but I really don’t recommend this.Basic Compilation: javac, gcc, g++, and gmake 27The Java compiler. The compiler looks in the same places for .class files, but its lifeis more complicated, because it also has to find source files. By default, when it needs tofind the definition of a class A, it looks for file A.java in the same directories it looks forA.class. This is the easiest case to deal with. If it does not find A.class, or if it does findA.class but notices that it is older (less recently modified) than the corresponding source fileA.java, it will automatically (re)compile A.java. To use this default behavior, simply makesure that the current directory (‘.’) is in your class path (as it is in our default setup) andput the source for a class A (in the anonymous package) in A.java in the current directory,or for a class 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., thosecreated by the compiler) in three different directories, if you really want to (I don’t think we’llneed this). See the -sourcepath and -d options in the on-line documentation for javac, ifyou are curious.1.2 Multiple classes in one source fileIn general,


View Full Document

Berkeley COMPSCI 61B - Basic Compilation

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 Basic Compilation
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 Basic Compilation 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 Basic Compilation 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?