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 package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package" Watch "package" New topic
Author

package

Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
I have finally decided to start using packages in my own code. I thought I knew how they work, I've successfully compiled and run many programs written by others that used packages. But I just cant seem to get them to work in code I've written!
for example, consider the following source code:
package NoLDS.webstrms;
public class ServerStream
{
.
.
.
}
saved as a file "C:\NoLDS\webstrms\ServerStream.java" (win2k)
I open dos and type:
C:\>set classpath=C:
C:\>cd C:\NoLDS\webstrms
C:\NoLDS\webstrms>javac ServerStream.java
C:\NoLDS\webstrms>java ServerStream
Exception in thread "main" java.lang.NoClassDefFoundError: ServerStream (wrong name: NoLDS/webstrms/ServerStream)
.
.
.

didn't work, so then I change the package info in the source to:
package webstrms;
public class ServerStream
{
.
.
.
}
save the file in the same directory, same name.
then I open dos and type:
C:\>set classpath=C:\NoLDS
C:\>cd C:\NoLDS\webstrms
C:\NoLDS\webstrms>javac ServerStream.java
C:\NoLDS\webstrms>java ServerStream
Exception in thread "main" java.lang.NoClassDefFoundError: ServerStream
what is going on? If I take the package declaration out and set the classpath to "C:\NoLDS\webstrms" everything works perfectly.
thanks in advance,
-guy
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Two things.
Go to DOS and type >SET at the prompt and make sure that the classpath is really what you think it is (I don't want to admit how often I have gotten this messed up).
Then, where are you sitting when you type
>java ServerStream
?
If you are IN the lower package directory, the system may be looking for the directory structure to be under the directory that you are in.
Try being somewhere else and invoking the app.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
When I invoke java ServerStream, I am sitting where my original post lists...
C:\NoLDS\webstrms>
in both cases...
I dbl checked my classpath eacht time right after setting it... I'm notorious for typos.
Thank you for the suggestion of trying to invoke java ServerStream from different places, I hadn't thought of that. I thought you always did it from the directory the class with the main method was in. Unfortunately, it didn't solve the problem.
grr, this is frustrating...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Try this... from the

directory, type this:


The full name of the class needs to be specified on the command line. This includes the package name and the class name. The classpath needs to be specified starting at the directory before the package. These two things mess lots of people up. ( Including me... I've had this problem at least 3-4 times in the past when something wouldn't run... then I have to slap myself on the head and go, "D'Oh! The old package/classpath problem again!" )

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
Nice! thanks!
I invoked
java NoLDS.webstrms.ServerStream
from C:\ directory and it ran perfectly... I never knew you had to specify the package part of the class's name on the command line when invoking the java command.
David McCormick
Greenhorn

Joined: Jun 26, 2001
Posts: 5
or try this:
c:\>java NoLDS.webstrms.ServerStream
which uses the full package name. I find it's easier for me to understand how the class fits within its package structure this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package