Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages + classpath

 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic