Debugging: compilier issues


I'm getting a weird load of messages when I compile the file to bytecode.

The weird messages are the debugger working. The debugger tells you where you've got an error in your code. Here are some of the classics...

Screenshot: Error - package does not exist

The Class "System" has been misspelt with a lower case S, so the compiler thinks it's a different Class, i.e. one it doesn't recognise. Note that it both tells us the file/line the error is in (Test.java and line 5) and gives us the erroneous code. A caret (^) points, in this case, to the end of the problem bit of code.

Screenshot: Error - cannot resolve symbol

This time it's not the Class that's been mistyped, but another bit of the statement (println). Note in this case that the caret points to the start of the problem.

Screenshot: Error - ; expected

This time there's a missing bracket from the start of the main block. Note how the debugger makes a suggestion (you might want to add a ";"), however, in this case it's the wrong one, as we want a bracket! Whatever, it at least points the claret at the right bit of code. Note also that the first line 3 error generates 3 more errors, because the compiler is expecting stuff to come after the lost bracket and doesn't find it. These will disappear if we solve the first error. If your errors scroll off the top of your command line box, you should be able to right-click on the box frame top and alter its properties (see the Command Prompt tutorial). If you can't, you'll have to work through the problems backwards and try and work out what's going on.

Screenshot: Error - ; expected

Here we have the same error message, but in this case it's correct - we've missed a ";" off the end of one of our lines.

Screenshot: Illegal character

Sometimes the compiler won't like a character you've typed. In the case above, it obviously doesn't like the first quote mark. This can especially happen when you copy code from Microsoft applications (Powerpoint, Word etc.). One potential problem is "smart quotes". Microsoft products will often change quote marks as you type. Most keyboards produce quotes that go straight up and down, but, obviously, in a written document it is nicer if you have quotes which curl in towards the quoted words. Microsoft does this replacement for you.

If you then copy the code into a normal text file and try to compile it, you'll get a hideous error because Java expects standard quotemarks. This is easily resolved - just delete the quotes and retype them.

Microsoft applications also replace other characters as you type - hyphens, for example (in some situations), and the ellipsis marks "...". However, these are less likely to come up in coding.

If you are compiling multiple classes, you may see this:

Screenshot: Error - can't find symbol

This is usually because the "classpath" hasn't been set up properly (this is where the JVM looks for the class files), and the JVM can't find the file. There are two solutions to this. The first is to put the classpath into what you type to do the compilation and interpretation:

javac -classpath . TheClassname.java
java -classpath . TheClassname

The "." just means "look in this directory that I'm compiling from first". The alternative is to set up the classpath for the computer. You can find details of how to do this in the JDK tutorial. Remember to restart your command prompt after altering this variable or the new one won't be picked up.