Next page | Contents page |

Beware of the classpath!

The commonest cause of failure to run is that the JVM cannot find classes that are needed.

If running from command line, first need this command:

set classpath=.;path1;path2;...

where pathN is either a folder or the name of a .class or .jar file; dot means current directory.

Note that the syntax of that command is platform dependent. On Windows use semicolon (;) to separate each path but on other platforms use colon (:).

Can instead use a switch in the java (run) command itself, but then the command line may become very long:

java /classpath=.;path1;path2 MyClass

Can also put files in jre/lib/ext and the JVM will find them automatically. There are drawbacks to this approach though - documented on the internet.

When running from within an IDE, the classpath is set up for you so you are not aware of it - one reason why I prefer to start the course using a plain text editor. If you distribute an application you must remember to set the classpath.

Next page | Contents page |