File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes more on packages Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "more on packages" Watch "more on packages" New topic
Author

more on packages

Richard Gibson
Greenhorn

Joined: Mar 16, 2000
Posts: 9
I too am having trouble with packages.
I write something like:
package uk.co.mywebsite;
at the top of my file and then create on the HD the folder uk, with the subfolder, co, with the subfolder mywebsite
The compiler can't find it.
I have added C:\ to autoexec.bat (I'm running Win 98)and my understanding was that the compiler started at one of the points stated in autoexec.bat and then followed the package name down substituting a backslash for the point...
I also tried the following from Bruce Eckel's tutorial:
package mypackage;
public class MyClass
{
public void prt(String s)
{
System.out.println(s)
}
}
and saved it as MyClass.java. I didn't compile it.
Then I created:
import mypackage.*;
public class Test1
{
public static void main(String[] args)
{
MyClass m = new MyClass();
m.prt("hello");
}
}
I saved this in the same directory as the other file and I get the following:
Package mypackage not found in import.
import.package.*;
I'm doing something wrong: what is it? Let me know if you need more information
Theresa Duick
Greenhorn

Joined: Feb 16, 2000
Posts: 27
Richard,
Your code lines are written correctly. You said that you didn't compile MyClass before proceeding with the next lines of code. To call MyClass within a different class, MyClass must be compiled first or otherwise it won't work within another class.
With importing the package, is Test1 located within the same folders as the ones you listed above? If it is, then it's not necessary to do the import. Did you check with your hard drive to see whether the folder & subfolders saved the way you wanted them?
Theresa
Richard Gibson
Greenhorn

Joined: Mar 16, 2000
Posts: 9
Theresa, thanks for helping...
Right,
I have compiled MyClass to create MyClass.class which is sitting in C:\my documents (or, in DOS, C:\mydocu~1)
I then try and compile Test1.java (as written above) and get:
Test1.java:1: Package mypackage not found in import.
import mypackage.*;
1 error
Test1.java is, of course, also in C:\my documents
...
Theresa Duick
Greenhorn

Joined: Feb 16, 2000
Posts: 27
Richard,
Have you resolved this problem yet?
Theresa
[This message has been edited by Theresa Duick (edited May 06, 2000).]
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
Where is your classpath pointing? If, for example, your classpath is pointing to c:\java\lib then your package must be in c:\java\lib\uk\co\mywebsite
Richard Gibson
Greenhorn

Joined: Mar 16, 2000
Posts: 9
There seem to be more problems with the Classpath than anything else around here!
Right: I have added the line mentioned in my post above to Autoexec.bat. This, I thought, told the computer to start looking for any requested files at one of the places mentioned (C:\WINDOWS; etc...). This is the CLASS (...something...), but it seems that this is NOT the Classpath command. I am running Windows 98 (if this makes any difference to what I should be doing). I would *like* to be able to get the computer to search from C:\ down (then I could create new packages etc.. starting from C:\ which is easy to remember!). Can you tell me how?
Sorry about all this - after all, it's not a JAVA problem, per se, but... though I may be, I live in hope that you kind and wondrous souls (have I begged enough yet? Can I get up off my knees now? )...
thanks in advance,
R
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
I'll assume you have available the two code examples below,
but both starting with "package co.uk.mywebsite" in some
directory somewhere (I use c:\wherever\it\lives, below). Follow
the following steps:
<pre>
set CLASSPATH=.;%CLASSPATH%
c:
cd \
md java
cd java
md example
cd example
md co
md co\uk
md co\uk\mywebsite
copy c:\wherever\it\lives\MyClass.java co\uk\mywebsite
copy c:\wherever\it\lives\Test1.java co\uk\mywebsite
javac co\uk\mywebsite\MyClass.java co\uk\mywebsite\test1.java
java co.uk.mywebsite.Test1
</pre>
Note that:
1. Both the javac and java commands should be executed from the "root" directory of your project (in this case c:\java\example).
2. Source files should live in subdirectories corresponding to their packages.
3. The full relative path names of the source files should be given to javac.
4. The full package names of the class files should be given to java.
5. The classpath should include "." (ie the current directory). You can type the "set CLASSPATH=;%CLASSPATH%" in each time, or you can put it in autoexec.bat
Although there are other possibilities, this is an approach which is (I hope) relatively simple to understand, It is also proof agains a lot of common confusions - you can start a new subdirectory of c:\java for each new project you start, and know that you won't accidentally use old class files from another project.
Please try this set up and let us know how you get on.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Richard Gibson
Greenhorn

Joined: Mar 16, 2000
Posts: 9
Frank, thanks for the line by line help. I tried it all and all the way to javac... was fine. They compiled. But when it comes to runtime I get an error message.
I think, however, at this point it's best if I read around the subject again, find out for myself about the CLASSPATH and get back to you. I'll post another message if I still haven't had any luck in a week - or even if I've finally cracked it (you never know, it might even be useful to someone else
Thanks for your help Frank, Tom, and Theresa - and don't worry: I've left Chapter 5 on the floor somewhere and moved onto to Chapter 6,7,8...
R
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: more on packages
 
Similar Threads
importing other classes and using classpath
Package Problem
"protected" error
Another CLASSPATH problem
basic Q.