Sun Java Solaris Communities My SDN Account
 
New to Java Programming Center

New to Java

Programming Center

Java Platform Overview | Getting Started | Learning Paths
References & Resources | Certification | Supplements




Contents
BACK<<Compiling Code | Troubleshooting

Troubleshooting Guide

This page describes the most common problems for compiling and running packaged applications, and the solutions to these problems.

Problems Compiling

Problem 1: Compiling Only the DiveLog.java File

Because the Dive Log consists of multiple files, it is packaged using the package keyword. This tells the compiler where to locate the class files for the application and the packages the Dive Log uses. Unlike small one class applications, you can't just use the command javac DiveLog.java. If you do, you get an error that looks something like this:

DiveLog.java:60: cannot resolve symbol
symbol  : class Welcome  
location: class divelog.DiveLog
                             new Welcome(),
                                 ^
DiveLog.java:66: cannot resolve symbol
symbol  : class Diver  
location: class divelog.DiveLog
                             new Diver(),
                                 ^
DiveLog.java:71: cannot resolve symbol
symbol  : class Dives  
location: class divelog.DiveLog
                             new Dives(),
                                 ^
DiveLog.java:76: cannot resolve symbol
symbol  : class Statistics  
location: class divelog.DiveLog
                             new Statistics(),
                                 ^
DiveLog.java:81: cannot resolve symbol
symbol  : class WebSite  
location: class divelog.DiveLog
                             new WebSite(),
                                 ^
DiveLog.java:85: cannot resolve symbol
symbol  : class Resources  
location: class divelog.DiveLog
                             new Resources(),
                                 ^
6 errors

Solution to Problem 1

To compile a packaged application you must include the classpath which points to, but does not include, the directory where the files live. If your Dive Log files are in the following directory:

C:\Applications\divelog

Step 1: cd to the divelog directory.
Step 2: Compile the Dive Log application with the following command:

javac -classpath C:\Applications\ DiveLog.java

Note the space after C:\Applications\. Because you're in the divelog directory, the compiler knows where to find DiveLog.java. The divelog package name in the file itself tells the compiler where to look for any other classes that you created as a part of that package.

Problem 2: Leaving off the .java extension

If you leave off the .java extension for the DiveLog.java file, you get this error:

invalid argument: divelog.DiveLog

Solution to Problem 2
Always include the .java extension, so the file name reads DiveLog.java when compiling.

Problem 3: Misplaced Curly Braces

Misplaced curly braces are the most common cause of compilation errors. It is easy to lose track of which braces go with certain blocks of code, especially nested blocks that contain if/else statements or similar constructs.

Leaving out or having an extra curly brace in your code can lead to a variety of compilation errors that give little indication of the real problem. At other times, the compiler may highlight a brace, saying you need to include another.

Solution to Problem 3

Comment braces as you create them. Mark the opening brace with a comment, such as {// Opens class, or {//Opens buildMenu method. Do the same with all closing braces, such as }// Closes class, or }//Closes buildMenu method. This helps keep track of all braces and to ensure you don't have extra or missing braces. Example code with comments.

Problem 4: Calling Methods from the Wrong Place

The Dive Log application calls all its methods from with the constructor or from within the main method. If you try to call a method outside the constructor or some other method, you get a compile error.

Solution to Problem 4

Defining and calling methods can be tricky, so to simplify, only the DiveLog.java file contains a main method, and all the classes contain a constructor that calls the methods. The methods are called from within the constructor, and they are defined outside of the constructor.

Step 1: Check the curly braces to be certain there aren't extra or missing braces.
Step 2: If the method is being called outside the constructor, move it inside the constructor.
Step 3: Make certain the the methods are clearly defined outside of the method or constructor calling that method.

Problems Running the Application

Running the Dive Log application is similar to compiling, but troubleshooting can be more difficult. If your classes compile without error, but the application does not run correctly, determining the problem can be frustrating and difficult. Below are some of the more common problems.

Problem 1: Trying to Run Only DiveLog.java

If you run the DiveLog class and this error appeared on the command line:

Exception in thread "main" java.lang.NoClassDefFoundError: DiveLog/java

You probably tried to run the application with:

java DiveLog

For a single class application that command normally works. For a multi-class, packaged application it does not.

Solution to Problem 1

The rules for compiling the Dive Log apply to running it as well.

To run a packaged application you must include the classpath which points to, but does not include, the directory where the classes live. Do not include the package name with the dot, though. If your Dive Log files are in the following directory:

C:\Applications\divelog

Step 1: cd to the divelog directory.
Step 2: Run the command to compile the Dive Log application with the following:

java -classpath C:\Applications\ divelog.DiveLog

Note the command to run is slightly different from the compile command:

javac to compile
java to run

Also, note the space after C:\Applications\

Problem 2: Naming the Directory Incorrectly

Similar to problem 1, the application needs to know where the class files are to be found. If you leave off the directory name in front of the file name, such as:

java -classpath C:\Applications\ DiveLog.java

you get an error similar to:

Exception in thread "main" java.lang.NoClassDefFoundError: DiveLog/java

You also get this error if you include the directory where the class files live in the classpath. Remember, you must cd into the divelog directory, then name the directory as a part of the class name to be run:

java -classpath C:\Applications\ divelog.DiveLog

If you still have problems compiling or running the Dive Log application after reading this page, use the Reader Feedback form. Include the command you used, and the error it generated. Problems and solutions will be added to this page to help all readers.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.