File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes HelloWorldSwing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HelloWorldSwing" Watch "HelloWorldSwing" New topic
Author

HelloWorldSwing

Charles Hildebrant
Greenhorn

Joined: Sep 28, 2007
Posts: 7
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


Cheers<br />Charles<br /> <br />Sometimes we live no particular way but our own.<br />~ Robert Hunter, Grateful Dead
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

GenRocket - Experts at Building Test Data
Charles Hildebrant
Greenhorn

Joined: Sep 28, 2007
Posts: 7
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Sep 28, 2007
Posts: 7
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HelloWorldSwing