# Java and main thread error



## Kris (May 11, 2002)

I'm wrinting a very simple Java Application for my Mac OS X with Swing. The compilation goes well, but I can't run it, due to an error. I can't seem to figure it out, and it's drivin me..  

This is the error I keep getting: *Exception in thread "main" java.lang.NoClassDefFoundError*. I tried to run an application I had written earlier (in the Classic days, with the awt package instead of Swing) and I got the same error. I have the newest (1.3.1.) version of Java, so everything should be alright. I suppose there's only a small thing that need to be fixed. Does anyone know what's going on or how ot can be fixed?


Kris


----------



## iconara (May 12, 2002)

this is the most common of all java problems:

> java X
Exception in thread "main" java.lang.NoClassDefFoundError: X


that means that there is no class named X in the classpath. in turn, the last sentence means that you are not running your application the way you should.

In java, one can have one's application in a package, let's call ours "net.iconara" (it's customary to make it look like a reverse domain name...) then we run our main class, let's call it "App" like this:

> java net.iconara.App

if we are in the directory that contains the directory "net" and that directory contains the dir "iconara", which contains the file "App.class".

If we are not in that directory, we must ensure that java finds it anyway, it can be done like this: say that the "net" directory is in a folder called "Developer" in your home directory and that your username is "iconara":

> java -classpath "/Users/iconara/Developer" net.iconara.App

if you don't have your application in a package (you should have!), do it like this:

> java -classpath "path/to/classes" App

and set "path/to/classes" to the actual path where you store your classes.

There are other ways of doing this, one way is to wrap up all your classes in a jar-file (with the "jar" tool) and add that file to your classpath.

you can have multiple directories and jar-files in your classpath, just separate the entries with a colon as such:

> java -classpath a/path/to/something:lib/test.jar:/Users/theo/Library/Java/ant.jar main.class.name

(where main.class.name is the name of your main class).

did I make any sense, or was it not what you asked? don't hesitate to ask again


theo


----------



## DrBozze (May 12, 2002)

I'm assuming that you are trying to run the application from the same directory it's main class is located in. The problem you are describing sounds like a standard classpath problem. Your classpath is somehow incorrectly set and the JVM can't find the class you specified as your main class. If you have no classpath set it should automatically search for classes in your current working directory.

Try typing <b>echo $CLASSPATH</b> in the terminal. If it is set, make sure "." (current working directory) is present in the classpath. If you're using the classpath argument to the java command you need to make sure it's OK too.

Hope this helps a bit.


Oops! Someone beat me to it. Sorry for the redundant post.


----------



## Kris (May 12, 2002)

Thank you both for your replies.  
But I'm still a bit confused, so I'll ask again: 

Let's say my program's name was MyProgram, and I decided to use almost the same name as a packagename: "My.Program". What does that _actually_ mean, two directories, one called "My" and another one inside called "Program"? Would I place all the files inside this folder? Would this make up a package? (Why is packages so great?)


How come it's doesn't work to run the program if you type to whole path, like /Users/kris/Java/MyProgram? (That works ok with other programs, such as Terminal Scripts.) 



Kris


----------



## iconara (May 12, 2002)

You want to have your classes inside a package so that you they live inside their own namespace. consider a company that develops a java-library with a class called Vector (let's call the company "Sun"). Then company b makes a mathematical library with a class called Vector.

Company A's Vector class is a kind of array, whereas company b's Vector class is representation of a mathematical vector. Now, if both companies releases their libraries without putting the classes in packages you could not use the libraries together since there would be two classes with the same name, and the compiler would be very mad at you. It wouldn't know which one to use.

In C++, Objective-C and other less elegant, object-oriented languages, the problem is solved by a convention that all library-class-names are prefixed by two or three letters that signify the library. For an example, all classes in Cocoa are prefixed NS (for NeXTSTEP, which is the company that wrote the thing from the start). The Java way is so much more elegant: separate classes in packages and have each package define it's own namespace, then classes do not need to be called "NSString" but simply "String".

You could see a package as a library. If we work for company "ABC" then all our classes are probably in the library called abc or com.abc and the classes are placed in a directory called abc or com/abc. Then, if we work on a library that deals with math, we would presumably call it "math" and place it inside the library/package called abc or com.abc, like this: abc.math. Then we decide to put our utility classes in a package called "util" and we have: abc.math.util. The files will be placed in a directory structure looking like this: abc/math/util. Define a class called Vector and place the file inside the correct package and we have (after we compile):
abc/math/util/Vector.java and abc/math/util/Vector.class. Nice and clean.

The math-package has a main-class (called so because it contains the main-method that is the entry point of the program) called "Main", placed in the abc.math package, then we run the program like so: "java abc.math.Main". Now Java will look for a file named Main.class in a directory called "math" in a directory called "abc" in the current directory. If it does not find one, it will return the error message in your first post. If you try to run "java Users.kris.Java.MyProgram", java will look for a file called MyProgram.class in Users/kris/Java (notice that the path is relative from the current directory) that is defined in the package Users.kris.Java. It will not find one.

I have probably just made it worse with this answer... I suggest some reading, try some of the Java tutorials at http://java.sun.com, or elsewhere on the web. Usually the package-thing just makes sense after some time, it is one of the more unique features of Java and is not always so much talked about (I have studied Java at university level, and the teachers never mentioned packages...). It is, however, considered bad manners not to use packages in Java.


----------



## Kris (May 14, 2002)

iconara: Thank you!
I fainally got it.. at least a bit. I'm not quite used to the packages yet, but I suppose it comes after a while. Again, thank you. 


Kris


----------

