Take the 2-minute tour ×

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?

share|improve this question
You just have a single file, why not remove the package name for now? It works fine for me when I remove that. –  Amit Jan 26 '10 at 9:52
add comment

4 Answers

up vote 4 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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":

share|improve this answer
add comment

Perhaps you compile and run with diferent java version. This is common when you try to execute an example at eclipse.

share|improve this answer
Hi welcome to stackoverflow. This question was answered almost 3 years ago and the problem was already identified. Next time please first read the existing answers and check if one has been accepted or not before you provide an answer on an old question. –  Rutix Dec 14 '12 at 20:18
add comment

Your Answer


