• Post Reply Bookmark Topic Watch Topic
  • New Topic

Package Statement  RSS feed

 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I need help with the package statement!
I have created a simple class that I would like to import into other programs as needed. The code for class PrintHeading is below.

The directory structure where this file is saved is as follows:
root\brianalbin\tools\PrintHeading.java
where root is a directory path in my Classpath environment variable.
Here is a program which attempts to use the predefined class PrintHeading.

Class Test is saved in the following directory structure.
root\Test\Test.java
I compile the code from the root directory as follows.
javac -classpath "." Test\Test.java
This works fine and a Test.class file is created.
Now when I execute Test.java from the Test directory I get the following message.
java.lang.NoClassDefFoundError: brianalbin.tools\PrintHeading
What I am doing wrong? Thanks for anyhelp you can provide
Brian Albin
[ March 09, 2004: Message edited by: Brian Albin ]
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,
I think you could add both the paths(root\brianalbin\tools and root\Test) to your classpath and then try running the Test class from the root directory itself. That should work.
There are other ways I'm sure but this works for me
Cheers!
 
Gregor Slokan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
You should run your program from root directory. Declare "Test" class also as part of package (in your case Test), recompile (from root with javac -classpath "." Test.Test.java ) and run (from root withe java Test.Test)
Note: Pacakge name usualy begins with lower case.
Have fun!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nanook,
Welcome (back) to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Albin:
Now when I execute Test.java from the Test directory I get the following message.
java.lang.NoClassDefFoundError: brianalbin.tools\PrintHeading
What I am doing wrong? Thanks for anyhelp you can provide
Brian Albin
[ March 09, 2004: Message edited by: Brian Albin ]

1st, you cannot execute Test.java. It is source code. The compile step would have created a Test.class.
When you try to execute it from Test directory as java Test, then it will require all the classes in the Test directory. If you want to reference classes in other directories then you need to specify the classpath for run time.
java -classpath <list_of_directories> Test.class
where directories in list_of_directories must be delimited based on OS.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you are typing the command line correctly?
Maybe you hit "/" instead of "." I don't seem to recall there being any slashes in any of my ClassNotFoundException stack traces.
Also, is this being cross-posted? I seem to recall already responding to this one .... maybe the heat is making me see things.
 
Brian Albin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if it seemed like cross-posting.
I thought it might be better suited for the intermediate forum, so I re-structured the way I was saving and compiling the files and asked the question again.
I did manage to solve my problem and I appreciate any and all responses I received. Please see the similar question I posted in the Intermediate forum for details. Thanks again.
Brian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!