Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Package Directives?

Status
Not open for further replies.

Dilenger4

Programmer
Nov 28, 2001
13
US
Does anyone know how i can use packages to subdivide my .class files? Some reason i can't get it to work. Say if my main directory where my files are stored is C:\Java. If i put package Java; at the top of a file and compile then run i keep getting "NoClassDefFoundError". My CLASSPATH environment variable is set in the autoexec.bat file to the directory C:\Java so even if i try and run using this "C:\java -classpath C:\Java; myFile" it still fails to work. Any suggestions?
 
the best way to explain this is like so: package java implies that your code will be found in the classpath if a folder called java exists when appended to the classpath

what your compiler is actually looking for is
c:\java\java
the second java being your package, the first set in the classpath.

to run this properly, set calss path to c:\, and write your main java class in the c:\. that way the package java will map to the folder java off your c drive.
 
Ok say if i want subpackages off of Java. What would i do then? Should i set the CLASSPATH to C:\Java? Im not too sure. Thanks for the help!
 
"what your compiler is actually looking for is
c:\java\java"

Right. It would be looking for a subdirectory Java under Java which i dont have. But i do have other subdirectories under C:\Java. For instance i have C:\Java\MyClasses now if i put "package MyClasses;" at the top of a file and try to compile i keep getting the same thing "NoClassDefFoundError"
This is what i don't understand.
 
yes, leave your class path at c:\java.

you can have either Java or MyClass as your package name, it doesn't matter as long as the case is the same for the folder name as it is in your source.

once your class path is set and you have compiled your program, you can access it from anywhere in you main. ensure your main isn't in some other package, otherwise its getting messy
 
I don't know it dosen't seem to be working. There must be another problem. The files are stored in C:\Java\Classes
every file has "package Classes;" at the top. The files compile fine. The CLASSPATH in the autoexec.bat is C:\Java
but i still cant run. Im stumped.
 
a little trick to get your classpath working, add . as the first entry in your classpath variable. this means the current folder will always be in the classpath.

so you should have the following in your classpath now
classpath=.;c:\java

give that a try
 
I don't know why I didn't think of this before... but it's a bad idea to use the &quot;java&quot; package in your own code. It's reserved by SUN. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
hey thanks guys. sorry i didn't reply back sooner. My modem software got corrupted. I switched to an IDE and just configured the classpath from there. I kind of got sick of coding on notepad. thanks for the help.
 
Your class path should point 1 level above the package.
working
|-package1
|-package2
To refer to a class in package 1 would be package1.theClass.
Also you code in that package must have the line
package package1;
before the class def.
You can refer to a file structure inside a jar the same
way. com.mystuff.package1.myClass means that the directories
looked like
working
|com
|mystuff
|package1
when you jar'ed it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top