Forums Register Login

package

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 818 times.
Similar Threads
Issues with cmd java command
Package problem, Help!
HelloWorld NoClassDefFoundError
Package
Main Class Not Found in JAR file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:23:48.