Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Dependencies/weird problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dependencies/weird problem" Watch "Dependencies/weird problem" New topic
Author

Dependencies/weird problem

Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi,

Not sure if this falls under "Beginning Java" or "IDEs version control and other tools" forum. Let me know for future ref if it's the latter.

Anyway, the prob I'm having is this:

I have a folder with .class files in it, and I added it to the build path in my Eclipse project. I can actually use the classes in that folder inside the editor i.e. class Picture for example, I can actually write <variable-of-type-Picture>. and the IDE will let me choose from all the methods and stuff defined in the Picture class.

When it comes time to run the thing though... I get: Exception in thread "main" java.lang.NoClassDefFoundError: Picture

There are no errors displayed by the IDE before running... if I hit Ctrl+Shift+O, all imports are added automatically.

One thing I'm finding a bit odd is that the .class files (which I can open and view in the IDE... i.e. I can view the source) have no package definition in them. But when I import them from classes I'm making, they come out like this: import javasource.Picture;

i.e. with a package. Also, in the package explorer, under Referenced Libraries, I have the source folder and the classes in it are in packages.

But anyway, how can the IDE be recognizing the classes, have no problem with me using them in my code, and then blow up when I run the thing???

Thanks,

Justin
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Welcome to JavaRanch!

Are you trying to instantiate a Picture object, or use one of the Picture class' methods?

Does Eclipse give you an error?

What happens if you try running from the command line?

Also: I think (not sure) when you use eclipse, you might need to set a classpath in your system properties.... maybe another rancher knows for sure.


When you do things right, people won't be sure you've done anything at all.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Justin Calleja wrote:One thing I'm finding a bit odd is that the .class files (which I can open and view in the IDE... i.e. I can view the source) have no package definition in them

Classes in a package cannot use classes in the default package (i.e. without a package statement). If you need to use the Picture class in any class other than one in the same default package, you will need to put it in a package and either import it or use a fully qualified name when referring to it.
Justin Calleja wrote:. But when I import them from classes I'm making, they come out like this: import javasource.Picture;

Must be an Eclipse thing. I don't use Eclipse.

In general, it is recommended that any class be placed in a package.


luck, db
There are no new questions, but there may be new answers.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Janeice,

Welcome to JavaRanch!


Thanks

em yeah, I'm trying to get the code that came with the book "Problem Solving with Data Structures Using Java a multimedia approach" (by Mark Guzdial and Barbara Ericson) to work in my IDE... but there's been nothing but probs.

I think I just have to research this issue a bit more by myself cos I'm not sure what's going on exactly... I had managed to get some stuff to work before (quite a while ago now tho so I forgot exactly what I did). I think I did something like link the source instead of adding a class folder to the build path. Then, in the linked source folder (i.e. a folder in my project which links to another folder on my machine), I created another folder so that I could copy-paste all java files to this new folder. That way they all got "package newfolder;"
... because all the files were in a default folder... and I think that was giving me probs at the time.

Anyway, after doing that it had worked... but then messed up somewhere else... whatever. I've tried looking for a forum of the book itself but couldn't find any... I posted here because of the Eclipse recognizing the class but blowing up with a no class found error... that's a Java thing (nothing to do with the book).

btw, here's the code if you're interested: http://coweb.cc.gatech.edu/mediaComp-teach

It's the Java-Sources.zip I'm having trouble with.

oh yeah:

Are you trying to instantiate a Picture object, or use one of the Picture class' methods?


I'm able to get away with instantiating a Picture object at compile time i.e. Eclipse is ok with me doing that (so it should mean that it knows where to look to find the Picture class when I run the thing... but I guess I'm missing something cos it's not finding the class... Exception in thread "main" java.lang.NoClassDefFoundError: Picture ).

What happens if you try running from the command line?


em... I haven't tried that, but I'm gonna keep looking at getting it to work in an IDE. I don't know how to use the cmd line other than for simple classes which don't depend on anything else... to run it from cmd I'd need to be able to tell javac where to find:

1. the folder on my machine which has the .class files which the .java i'm compiling uses.
2. be able to include jar files which it might need to compile

Anyway, I'm getting a bit carried away here. I'll look into this some more.

Also: I think (not sure) when you use eclipse, you might need to set a classpath in your system properties.... maybe another rancher knows for sure.


Ok cool, thanks

So basically, I think the prob is I don't understand Eclipse well enough. Gonna look up on that, thanks

Regards,

Justin
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Darryl,

Classes in a package cannot use classes in the default package (i.e. without a package statement). If you need to use the Picture class in any class other than one in the same default package, you will need to put it in a package and either import it or use a fully qualified name when referring to it.


wow thanks pal, I knew it was a bad idea to put classes in a default package but I just did it (i.e. put classes in a package) without really thinking why. If you think about it, it makes sense... what import statement would you use to import the classes in the default package? lol

hmmm thinking about it now... even though it's a bad idea to work in the default package ... I should probably work in the default package with the code that came with this book (ref to my 2nd post). Because, I can't use the .class files since they were generated from .java files without a package statement, and there are quite a bit of .java files I'd have to go through and edit (i.e. give them a package statement) and then compile, so that I'd be able to use them (finally).

But just working in the default package might just solve everything straight away... k ill try that.

cheers Darryl

Regards,

Justin
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Justin,

I try to urge folks away from using an IDE until they understand what's going on for themselves. Using an IDE doesn't make you "good at Java" any faster than anyone else.... you become good at the IDE.

Writing programs in a text editor, learning to compile them, fix compilation errors, understand what's going on..... that gets you better at the language. Using an IDE adds confusion, complexity, and unnecessary issues to the learning of the language. You need a good foundation before you can start building on a higher level.

It's like when you learn digital photography.... you start with a simple camera and learn to point and shoot. You learn about lighting and angles and how to make stuff look good. THEN you get the expensive SLR camera with all the bells, whistles and extra features.... then you learn to master the features.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Janeice,

yes that's ture ^^;

I definitely need to learn to use the cmd line properly... do you know of any good resources for that?

And what exactly should I be able to do with the cmd line before moving on to an IDE? I know how to do basic stuff like compile and run. It's the dependencies and stuff which me.

Thank you,

Regards,

Justin
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Guess you checked
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html

if not have a look.


www.cs-repository.info
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi John,

ah yes I had managed to get a class which depended on another to work through the cmd line once... you'd have to add the classes you depend on temporarily in the classpath right. ...hmm a lot of playing around, but yes I should check it out.

Thanks

Justin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependencies/weird problem