Compiler concepts
Q: What is the source code for the compiler?
A: The source code for a Java program is also known as a compilation unit, which contains the code for a top level Java class or interface. A Java compilation unit is usually created in the form of a file with a .java extension and is passed to the compiler as a file path reference.
The Java source file contains a header that declares the type of class or interface, its "visibility" with respect to other classes, its name and any superclass it may extend, or interface it implements. The body of the class contains variable declarations and methods that define the behaviour of the class, and any constructors used to create an instance of the class. A compilation unit may also contain nested inner classes.
Q: Why is the source file named after the class?
A: The Java source file naming convention is not a standard specified by the Java language but is a common feature of Java compilers, such as javac, to help locate source code. The source content of a Java class is known as a compilation unit. By storing the compilation unit in a file that is named after the class, the compiler can locate any supporting classes by name and compile those too.
This convention also extends to package names. Most Java compilers expect source code to be stored in directories whose names match their package hierarchy. Thus the source code for a class named Example in the package com.domain.util might be stored in a file with the path c:\src\com\domain\util\Example.java
Q: Which class should be compiled first?
A: Sometimes you will find that trial and error will give you the answer you need. If you are using the Sun compiler, javac, the compiler will compile any other classes that your target class depends on, provided the other source files are in the same directory hierarchy as the first and the sub-directory names reflect the package hierarchy of the classes.
Compiler commands
Q: What are the steps in compiling a class?
A: Once you have written the Java source code file, there is only one step required to compile it. To compile the class from the command line, you need to give the path to the compiler program, such as Sun javac, and the path of the source code file, like this:
Q: What do I type at the command line to compile Hello.java?
A: In the simplest case, type cmd in the Windows Run dialogue (on the Start menu) and change to the source directory then run the javac command, as below.
Q: Where is my compiled class file?
A: If you are sure your class is being compiled, then the class file should be output somewhere! Without any directory argument, your compiler should place the class file in the same directory as your source file. Use the output directory argument to specify where the class files are generated, as below.
Q: How can I compile Java classes with packages on the command line?
A: When you plan to compile Java classes into a package hierarchy it is best to organise your Java source files in a folder hierarchy that matches your packages and use the compiler's -classpath or -cp argument and output directory parameter -d to keep the class file output organised in a separate equivalent structure.
package one;
public class ExamplePackageOne {
public static void main(String[] args) {
System.println.out("Example output");
}
}
package two;
public class ExamplePackageTwo {
// Empty
}
Add the -d command before the Java source file reference, followed by the path for the output folder, and the -cp command followed by the common root directory for your source files. The examples below are split over several lines for clarity, it may help to put the commands in a batch script to experiment with.
Q: How can I ensure my compiler will locate the SAX package?
A: One way to ensure your compiler can locate any package it may require is to pass its path to the compiler explicitly using the -classpath argument, see the exmaple below.
Compiler diagnostics
Q: I get "javac not recognised"!
A: This error means that the Windows command shell cannot find the path to your Java Software Development Kit installation where the javac program is installed. One way to solve this is to add the javac program to the system environment variable named PATH. You can also give the full path to the javac program in the Windows shell, e.g.
c:\JavaSDK\bin\javac c:\projects\java\MyClass.java
To add to your PATH variable, open the System Properties applet in the Windows Control Panel, click the Advanced tab, then the Environment Variables button. In the System Variables list select the PATH variable and then the Edit button. Type a semicolon separator followed by the path to your javac program then click OK to save the changes and back-out. Now you should be able to use the javac program without specifying its path.
javac c:\projects\java\MyClass.java
Q: I get "cannot find symbol", what's wrong?
A: There is nothing wrong with the Java source you have given. Provided the two Java source files are in the same folder and it is the current working folder, when you compile the HelloApp.java class the Java compiler should find the source of the Hello.java class and compile it too.
The "cannot find symbol" error suggests that your Java source folder is not the current working folder, or you have given a -classpath argument that does not match the source folder. The first case is the most likely. For example, if your source files are at C:\Documents and Settings\Test and your command prompt is C:\> the implicit classpath is the root folder of the C: drive. This is where the compiler will look for any supporting classes and won't find them there.
To solve this problem add an explicit classpath argument for your test folder, split over two lines below for clarity. Enclose file paths with spaces in quotes.
C:\> javac -cp "C:\Documents and Settings\Test"
"C:\Documents and Settings\Test\HelloApp.java"
Q: What does this deprecation message mean?
A: The deprecation message you have seen means that the methods you are calling have been marked with a JavaDoc deprecation comment. When a method or class is marked @deprecated it is only advisory, not mandatory, but the advice is given for good reason and should be followed. So long as deprecated methods remain in the public API it is possible to use them, so this approach supports legacy code and gives developers time to amend their applications as necessary.
Q: What are undefined and undeclared variables?
A: One gets warning messages about undefined and undeclared variables when compiling Java classes that have programming errors, as in the example below.
Q: Why doesn't the compiler warn about stack overflow problems?
A: There are many cases of poor runtime programming that could potentially be identified at compile time, but the number and subtlety of the cases gets increasingly difficult to address. The main purpose of a compiler is to produce executable byte code that is valid according to the rules of the programming language, not to guard against poor programming. Hence most compilers only validate the syntax of the language and the most obvious logical errors in the code at compile time.
Development environment
Q: What Java compilers are there?
A: There are many Java compiler implementations. The one most developers start with is that provided with the Sun Java Software Development Kit (JSDK), named javac. Alternative implementations include the free open source GNU Compiler for Java (GCJ) and the Eclipse Compiler for Java, which is part of the Eclipse integrated development environment (IDE).
Q: How do I set the compiler path?
A: The Java compiler is a program like any other and your operating system needs to know where to find the executable file. The simplest way to do this is to give the full path to the Java compiler in the command, as below for Windows...
Q: The DOS command cd doesn't go to the Java directory!
A: The Windows command line program cd is to change directory, the present working directory for subsequent commands, and requires a space between program name cd and the directory path you want. The examples below show three main ways to change directory starting from the root of the C: drive.
REM Change to the root of the C: drive
c:
cd \
REM Relative path with leading slash
cd \jdk1.5\bin
REM Return to root of C:
cd \
REM Relative path, no leading slash
cd jdk1.5\bin
REM Return to root of C:
cd \
REM Absolute path
cd c:\jdk1.5\bin
Q: How do I set environment variables on Windows XP?
A: For Windows 2000/XP systems, the environment settings are edited in a special Control Panel applet called System.
Q: How do I configure EditPlus to compile Java and capture output?
A: These instructions on configuring the NSGMLS markup validator for EditPlus will help you get started. For Java, the Command field will be the path to your javac.exe program (or java.exe to run). The Argument should contain any parameters you want to pass to the compiler and should end with the $(FilePath) variable that substitutes the current file name. Check the Capture output box to get feedback from the compiler.
No comments:
Post a Comment