• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

HelloWorldSwing

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would seem to be the simplest thing but it is bothering me in two ways.

One is that something isn't working properly.

And two is that I can't seem to figure out how to fix it.

The source file comes from the sun.java website as follows:


This compiles with no complaints but when I try to run it I get this:


charles@linux-hoq0:~/javaJunk/HelloWorld> java HelloWorldSwing
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
charles@linux-hoq0:~/javaJunk/HelloWorld>



I've looked all over the place (including these forums) for answers and found many different suggestions. None of the suggestions apply in any way I'm aware of. In my book, Head First Java, swing apps aren't covered until chapter 12 and I'm only on Ch.3 so it's not a panic, but it still bothers me.

I have Java 1.5.0_08 on a 64 bit linux box (Open Suse 10.2)


Any help I could get would be well appreciated.

Thanks
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class is in the package start. Try executing it like this:

charles@linux-hoq0:~/javaJunk/HelloWorld> java -classpath . start.HelloWorldSwing

That's of course assuming your directory structure looks like this:


[ October 04, 2007: Message edited by: Gregg Bolinger ]
 
Charles Hildebrant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
INTERESTING

Actually, there are no more directories contained in ../HelloWorld/

I am unfamiliar with the line: [ package start; ]

Is this like the [ #include myFile.h; ] in C++?

If so I'm assuming "start" contains the classes I need to get this going, eh?

Should I set my envvar CLASSPATH to wherever this package is?

Is it in a jar file?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Packages in java are like namespaces.

Java Package.

A .class file must exist in a directory structure equal to it's package. So for your example there should be a folder start and the class file should be placed there.

For simplicity sake, remove the package declaration, compile, and try and run it without the start. on the front.
 
Charles Hildebrant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well sheriff, that seems to have done the trick.

That makes me wonder why that line was included in the first place. Probably just to confuse us greenhorns, I reckon. Seems like the author at Sun threw in that line the same way I'd always include the line in C++:

Only this time it referred to something that wasn't there, something the Sun programmer wrote, eh?

Thanks for you help, Gregg
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. I don't have that particular book but I know the authors. In fact, one of them started this dinky little site many many moons ago and then sold it to Paul Wheaton to run.

Just as an FYI:

The java equivalent to using namespace std; is the import statement, which I am sure you'll get into soon enough. The package delcaration is how a namespace is created. For example, say someone wrote a class you decided to use called MyCoolObject and you decided that was a pretty stinkin cool name and you want to name one of your classes MyCoolObject.

If we didn't have namespaces (packages) then it couldn't be done. However you might place yours in a package called com.charles and you would declare it as such:



Now the conflict is gone. And your .class file would exist in a directory structure:

/com/charles/MyCoolObject.class

You'll get it. Just takes time and lots of writing code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic