I have now got the package function to work
Just in case anyone else has been having the same problems, these were my mistakes:
1) I was saving the file CALLING the package in the same folder as the package's class files! This is like storing the reference book on the same shelf as the books it is referencing (i.e. not very useful!)
Here is my structure:
c:\uk\co\mysite
All of the .class files (e.g. Tools.class / etc...) are stored - and compiled - in the "mysite" folder
*each file has "package uk.co.mysite;" as the first line of code*
Then, I save the files which will CALL on these class files in c:\ - remembering to write "import uk.co.mywebsite.*; at the top of each one
The way for me to remember this is:
The first part of the structure (c:\) is where you store the files which will CALL on the package. The second part of the structure (uk\co\mysite) is what you will write at the top of the files you are creating IN your package.
If I chose to store the files which CALL the package in c:\uk then the top of the files in the package would read:
package co.mysite;
and the import statement would read:
import co.mysite.*;
and if I chose to store the files which CALL the package in c:\uk\co then the package line would read:
package mysite;
and the import statement would read:
import mysite.*;
--------------------------------------------------------------
To get it all to work I do the following:
1) Write the files which will form your package e.g.
package uk.co.mysite;
public class Tools {
public void prt(
String s) { System.out.println(s); }
}
2) At the command line, move to the directory where you have stored the files (c:\uk\co\mysite)and run (e.g.)
javac Tools.java
3) Move to the directory where the files CALLING the package will live (in this case c:\) and create them e.g.
import uk.co.mysite.*;
public class ClassCallingPackage {
public static void main(String[] args) {
Tools a = new Tools();
a.prt("it works!");
}
}
4) Type the following:
set classpath=.;
which means "java, start searching for the packages from here"
(classpath means "start searching" and "=." means "from here". The semi colon at the end means "and that's all for now". That's oversimplified, but it has taken me two weeks to get this far!!
)
4) Type:
javac ClassCallingPackage.java
The compiler will then look at the import statement and follow the folder hierarchy down to the last folder in the import statement and look at all the .class files there for one with the name used in ClassCallingPackage (in this case this is Tools.class) whenever they are used in your new file.
5) Pray that you don't get some horrible "class not found, you have got it all wrong, hang your head in shame" error message.
6)
You should now be taken back to the command line prompt as per any other javac operation
7) run
java ClassClassingPackage
8) and it WORKS!
Well it did for me
I had a LOT of trouble with this and if I can help anyone else get there a bit quicker...(I may WELL be coming back to my own message when I forget how to do it!)
R