This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes packages + classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "packages + classpath" Watch "packages + classpath" New topic
Author

packages + classpath

Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
Hi,

I have been working on something else for a while and have foolishly forgotten how to set up classpath/package structure properly as am getting class not found errors. I am on Windows NT.

I have altered autoexec.bat to include: set CLASSPATH = C:suncertify\db
The files I am trying to execute are located at: C:\suncertify\db
The package statement at the top of each file reads package suncertify.db;

Do I have to add another PATH or CLASSPATH statement to autoexec?
along the lines of: set PATH =j2sdk1.4_01\bin;

Thanks - sorry for the thick question but have been doing non-Java stuff for 5 months and forgotten this rudimentary thing.
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
I should add that all is well when I compile with:

javac -d . suncertify\db\*java

But when I try to run a class within the package (e.g. MyTest.java)
I get 'Exception in thread "main" java.lang.NoClassDefFoundError: MyTest
Peter Laurinec
Ranch Hand

Joined: Sep 13, 2004
Posts: 52
Try include "." which points to the actual directory to the classpath.
Usuall call to a class within the package is java package_name.subpackage.ClassName


SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Peter Laurinec:
Try include "." which points to the actual directory to the classpath.
Usuall call to a class within the package is java package_name.subpackage.ClassName


Advice:
Setting CLASSPATH to contain . or using -cp . will appear to fix all classpath sins, but it really just covers them up. The best approach is to build the jar file and run from it as specified in the project requirements.

To directly answer your question:
if your code is in C:\suncertify\db and you use "package suncertify.db" in main file Test.java, your classpath should be C:\. To run this you would do:
java -cp C:\ suncertify.db.Test

if your classes were in C:\javadev\build\classes\ and you had a mainprogram called URLyBird in package suncertify.gui and you were trying to run from F:\ you would use:
F:\>java -cp c:\javadev\scjd\build\classes suncertify.gui.URLyBird alone

if you packaged all this up in a jar file called runme.jar in the build directory you would use
F:\>java -jar c:\javadev\scjd\build\runme alone

and your manifest file would look like
Manifest-Version: 1.0
Main-Class: suncertify.gui.URLyBird


hope this helps /pkw
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: packages + classpath