• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

more on packages

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard,
Have you resolved this problem yet?
Theresa
[This message has been edited by Theresa Duick (edited May 06, 2000).]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Richard Gibson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paper beats rock. Scissors beats tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic