Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java could not find the main class?

Tags:

java

class

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?

like image 591
Roman Avatar asked Dec 05 '22 03:12

Roman


1 Answers

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.

like image 189
Michael Borgwardt Avatar answered Dec 07 '22 17:12

Michael Borgwardt