tags:

views:

462

answers:

3

I have just copied Key-Listener code from http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java. I was able to compalie it with the "javac" command. But when I try to execute the compiled code (typing "java KeyEventDemo") I have a large message in the end of which I see:

Could not find the main class: KeyEventDemo.  Program will exit.

Yesterday I had a similar problem on Windows Vista (now I am on Ubuntu). In the Windows I was able to solve the problem by typing "java -cp . ProgramName" or alternatively by adding new values ("." and "..")to the environment variable "classpath".

On Ubuntu the first solution does not work. I mean, when I type "java -cp . KeyEventDemo" I still have the problem. Moreover, on Ubuntu I was able to run other programs just typing "java ProgramName".

So, can anybody tell me what is special about this KeyEventDemo? Why it does not wont to work and how it can be solved?

+2  A: 

This program is not in the default package, but in the package "events": use java -cp . events.KeyEventDemo from the directory containing the folder "events":

   +work
    +events
     -KeyEventDemo.class
Jerome
+1  A: 

The class KeyEventDemo is in a package events To run it, you must be in the parent folder of the events folder that contains the class, and run it using its fully qualified name, including the package:

java events.KeyEventDemo

The classpath must contain the folder (or JAR) that's the root of the folder hierarchy that represents the packages; the current folder is (I believe) included automatically.

Michael Borgwardt
+1  A: 

It is because the KeyEvent class is in package events.

You either have to remove the package events; line from source code, or compile it with:

javac -d . KeyEventDemo.java
silk