DOC

JavaNote3-Errorsdoc - Richards Java Notes, number 0

By Scott Shaw,2014-04-24 12:39
58 views 0
JavaNote3-Errorsdoc - Richards Java Notes, number 0

Richard’s Java Notes, number 3 5 December 2005

    Some common errors and how to put them right

    Note that I didn’t say “and how to avoid them”. Avoiding errors completely is

    difficult. If you make mistakes, you can learn from them.

    I’ll be adding to this document periodically as I discover new errors. If you want

    more information about Java errors, including a full list of error messages, there is a

    web site with this information, which I have currently lost please type “java error messages” into Google, because that will show you several useful pages at the top of

    the list!

    Messages from the operating system

    These messages relate to the Java compiler or interpreter not being found, very likely

    because they were not installed or not installed successfully. If you receive any of

    these messages, see the end of this section for what you can do to try to solve the

    problem.

    ? 'javac 'is not recognized as an internal or external command, operable

    program or batch file This message comes from Windows when you try to run

    the Java compiler, javac, and means that Windows can’t find it. This might be

    because you haven’t installed the Java software development kit, or the

    installation did not fully succeed for some reason. If the Java software

    development kit has been installed, use the Windows “Search” function (on the

    Start menu) to find any file called “javac.exe”. If it can’t be found, you need to

    install the Java software development kit (again), or use a computer in COMSC

    where it is already installed. See below. If the file is found, the fact that you got

    the error message means that javac.exe is not in the “search path”, in other

    words it is not in one of the places where Windows looks when it is trying to

    identify a command that has been typed.

    If this is the case, one solution is to change the search path to add the folder where

    javac.exe and java.exe are installed, and reboot. The other solution is to

    explicitly specify the folder when you invoke the javac and java commands. If

    the Search for Files process shows that the javac.exe file is in the folder

    C:\Program Files\JavaStuff, for example, then when you want to compile

    a program, instead of typing “javac filename.java” and “java filename

    you should type “C:\Program Files\JavaStuff\javac filename.java

    and “C:\Program Files\JavaStuff\java filename”. The tedious nature

    of this solution is a reason why there is a search path! ? bash: javac: command not found This message is the Linux equivalent of the

    Windows message above, and means that when you try to run the Java compiler,

    javac, Linux can’t find it. This might be because you haven’t installed the Java

    software development kit, or the installation did not fully succeed for some reason.

    If it can’t be found, you need to install Java (again), or use a computer in COMSC

    where Java is already installed. See below.

    If you receive any of these messages, go to a Command Prompt or bash shell (that is,

    depending whether you are in Windows or Linux, the place where you can type

    commands), and type the following:

java -version

    javac version

    If the first produces a message giving details of the Java Runtime Environment

    version number, and the second one produces one of the operating system error

    messages above, then one possible explanation is that you, or something, has installed

    the Java “run-time” system (“JRE”) but not the “software development kit” (“JDK” or

    “SDK”), which includes the compiler. The run-time system does not contain a compiler, it will only run compiled “.class” files that someone else has compiled and distributed. If both commands fail, not even the Java run-time system was installed

    successfully. The remedy in both cases is to install the software development kit

    (called either “J2SE SDK” or “JDK 5.0” or a later version thereof see Java Note 0). Messages from the Java compiler

    These are some of the messages you may encounter when trying to compile your

    program with the javac command. Remember that “compiling” means translating

    your program from Java, which is really meant for humans, to the internal (“byte code”) form in which the program is actually run. ? cannot resolve symbol You have used a word that Java does not recognise. This

    may be because you have spelt it wrongly, or you have forgotten to specify or

    import the package containing its declaration.

    ? „class‟ or „interface‟ expected Probably you don’t have your braces (curly

    brackets {}) matching properly.

    ? expected Probably you don’t have your braces (curly brackets {})

    matching properly.

    ? illegal start of type Probably you don’t have your braces (curly brackets {})

    matching properly.

    ? „try‟ without „catch‟ or „finally‟ Probably you either forgot the catch after a

    try, or the braces (curly brackets {}) don’t match properly. ? unreported exception You need to handle an “exception” (possible error) that a

    method you have called may produce. Typically you would enclose the relevant

    code within a try catch block.

    The following messages start with something you have written, and hence can’t be

    sorted into alphabetical order.

    ? .java:: package com.mysql.jdbc does not exist

    Java cannot find the database driver you are trying to use with

    DriverManager.registerDriver()

    ? .java uses or overrides a deprecated API Java is trying

    to tell you you are doing something old-fashioned.

    Messages from the Java virtual machine (at run time)

    “At run time” means when you try to run your program using the java command,

    after compiling it with the javac command.

    ? Exception in thread “main” java.lang.NoClassDefFoundError:

     This happens if the name used in the class declaration

    YourJavaFileName, does not (the class that contains the main method), such as

    match the file name used to store the program (YourJavaFileName.java). In

    other words your program needs to start with

    class YourJavaFileName

    { public static void main (String[] args) …

    and be stored in a file called YourJavaFileName.java.

    If you’re sure this is correct, another possible cause of this error, in Windows, is

    that your CLASSPATH environment variable is not set correctly. See Java Note 0

    for how to fix this.

    ? Exception in thread “main” java.lang.NoSuchMethodError: main This is due

    to omitting the main method, and may be caused by accidentally trying to run an

    applet (which lacks a main method) as if it were a program.

    ? Main method not public You have forgotten to declare the main method to be

    public. You need it to start with public static void main (String[]

    args);

Please send me any more error messages you come across, with or without the way

    you managed to correct them!

Report this document

For any questions or suggestions please email
cust-service@docsford.com