Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package

 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic