• Post Reply Bookmark Topic Watch Topic
  • New Topic

Big problem with Sun online tutorial: Swing  RSS feed

 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The very first exercise in this trail is the compiling and execution of a little application called HelloWorldSwing. I have copied the code from the tutorial and pasted it into emacs. I then compiled it into the same directory into which I had saved the source code. The compile was clean; I checked to be sure that the source and class files were in the same directory (after the execution attempt bombed horribly). I have checked the FAQs in the tutorial; they are no help. Everything that I have copied from Head First Java as well as the stuff that I have written myself has run all right (within findable errors on my part, like null pointers). Could someone please suggest where I should look to get a handle on this?

I am using Windows XP. The errors are as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.define Class(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

The source code is:

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declared package is "start," so this this needs to be in a directory (folder) called "start." Once it's compiled with this package declaration, the fully-qualified name is start.HelloWorldSwing. Use this when running.

At the command prompt, change directories so that the current directory is the directory that contains start. For example, suppose the "start" directory is under your own directory called myJava. Then tell javac where to find the file by compiling with:

...myJava> javac start/HelloWorldSwing.java

And run by giving java the fully-qualified name:

...myJava> java start.HelloWorldSwing

Ref: JLS 6.7...
The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.

[ July 26, 2007: Message edited by: marc weber ]
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc -

ooooooops - there should be a Graemlin demonstrating extreme embarrassment!

I haven't got to packages yet in Head First Java - so hoped I could just ignore the "package" statement at the beginning. Now I know better. Commenting it out worked just fine!

With much chagrin,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinny Morris:
...hoped I could just ignore the "package" statement at the beginning. Now I know better. Commenting it out worked just fine! ...

Removing the package declaration will work for now, but don't wait too long to get familiar with packages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!