Java General Tips
For more advanced hints
-
javac: Command not found
Here your PATH variable is not set properly to include the javac compiler.
Here at Chico, the compiler is at /opt/java/bin
- In class HelloWorldApp: main must be public and static
A runtime error - the word static was left out of the line containing the main method
- Can't find class HelloWorldApp
A number of subtle reasons for this:- the classname was spelled differently
than the source file name, and a filename.class file was created
with the spelling(including case) of the class definition (in this case,
maybe your filename.java file defined class HelloWorldapp {}
and thus that is what it created, a HelloWorldapp.class
- check directory - are you in the right one, or does your CLASSPATH have
a link to this directory
- check permissions on the class - is it readable?
- Applications: If you store more than one class in a single file,
only the one with the
main(String[] args) method should be public
. More specifically, every public class should
be in its own directory.
- If you have defined your own package,
- all files of a package must be located in a subdirectory that matches
the full package name. (all files in the java.util package are in
a subdirectory java/util)
- the name of the package must be at the top of the source file before the code
- make sure that the actual
call to things in this package (i.e., the class with the main(String[] args) method) is not in the package (i.e., it does not have the
package packageName;
declaration in the class that uses it. Rather, that class should
import packageName.*;
- OR make sure that you call the class with the full path. For
example, if a class
myClass is in package myPackage,
then to run the application you should say
java myPackage.myClass
- Make sure that your CLASSPATH includes the directory
where the directory with your package(s) is located as well as the
current directory (. in the CLASSPATH). The current directory is always
the default package if none is specified.
- If you run the java -classpath path command to specify a CLASSPATH from the
command line, it overrides the CLASSPATH that was set by environment variables.
- If you are using an environment (such as JBuilder) you should
read the documentation to see if your GUIBuilder uses a default layout
manager. Most of these use no default rather than Java's
default of FlowLayout. Thus, what happens is that when you send me your
stuff, and I run it from the command line, your layout looks terrible
on my machine...it will only look alright on the platform you are using.
LESSON always use an explicit Layout Manager. (suggest GridBag Layout)
if you want your java code to be portable.
- If you want to provide someone with a .jar file through
a web page, you might want to change the name of the .jar file
and give it a .zip extension. Windows 95/NT machines do
not (apparently) send it as binary if it does not have the .zip extension
- Here is a weird one! I was trying to run appletviewer
on a URL for a students
program. I kept getting errors. Other people didn't. I messed and messed
around trying to find things that could be wrong. Guess guess guess guess
guess guess guess guess
I had a class with the one of the same classnames as his code in the directory
I was running it from, and I had . in my CLASSPATH. So it saw mine before
his. Pretty weird...pretty hard to find!
- Another weird hard one to find. Do not call a package and a class
in the package the same name...unless caps are different. In general,
usually packages are all lower case.
See also Common Compiler and Interpreter Problems (and Their Solutions) from the SUN tutorial (some of these are the same as above, some different)