This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

import & package

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi, everyone
I am a very very beginner in Java. I got confuse about the import & package & CLASSPATH. My questions are:
1. If I import a package, such as "java.lang.*", where can I find the path of "java/lang"?
2. If I import my own package, such as "com.myjava.tools", do I have to build a directory like "c:/test/com/myjava/tools"?
3. In my own package "tools" has two java files, base.java & derived.java
in both of them use:
"package com.myjava.tools" as the first line,
in base.java, I defined a "public class BaseClass"
in derived.java, I derived this BaseClass like this:
"public class DerivedClass extends BaseClass"
now if I compile derived.java, there's error of "can not resolve symbol: BaseClass"; then I have to build the directory "c:/test/com/myjava/tools" and put base.java there. then compile ok. But that's not what I want, because I want all the java files in the same package can stay in the same directory. And if I have more files and they derived each other, what can I do, how many "c:/test/com/myjava/tools"? I have to build?
4. Then I try put "c:/test" in my Classpath, but it doesn't help, even worse.
I totally confused about this path setting of import, package & classpath. How should I deal with them?
Thanks in advance!
ps: My English is poor and I am very new in Java, I hope I make myself clearly. Thank you very much for your patience of reading my questions.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Joyce!
I can answer your questions, but first, I'd like to point out our Naming Policy. Please change your display name to include two names of at least two letters each. Thank you.
Now, on to answer your questions:
1). If you import java.lang.*, you can find the path in the classpath of the JVM. However, don't go looking for java.lang in the CLASSPATH environment variable; the JVM will have access to the java.xxx packages when it starts, regardless of what the designated classpath is.
(If you're interested, the source for the java packages can be found in the JAVA_ROOT/src.zip file, but that's not where the classes are loaded from).
2). Yes, you do have to build that directory. Any files that belong to that package should be placed in that directory.
3). A couple of problems here:
a). Your file name must match your class name. So if you defined a

it must be in the file:
{CLASSPATH}/com/myjava/tools/BaseClass.java
Likewise, the DerivedClass should be in the
{CLASSPATH}/com/myjava/tools/DerivedClass.java
The reason for this is:
When you try to compile a class, the javac compiler will attempt to resolve all external symbols. Since you have declared that DerivedClass extends BaseClass, the compiler attempts to locate BaseClass. It looks up BaseClass's package (com.myjava.tools) and locates that directory in the filesystem. Then it looks to see if there is a BaseClass.class file there. If not, it looks to see if there is a BaseClass.java there--it will then compile the BaseClass.java file, producing the BaseClass.class file. But the BaseClass files must be in that directory.
As you undoubtedly found out, it is possible to compile a class in the wrong directory. (You compile BaseClass.java in c:\test instead of C:\test\com\myjava\tools\). The compiler won't complain about that, but when you try to either 1). Run that java program or 2). Compile another class that uses that program, you will receive an error.
So, in summary, your directory structure should look like:
C:\test\
C:\test\com\
C:\test\com\myjava\
C:\test\com\myjava\tools\
C:\test\com\myjava\tools\BaseClass.java
C:\test\com\myjava\tools\DerivedClass.java
and, after compilation, include:
C:\test\com\myjava\tools\BaseClass.class
C:\test\com\myjava\tools\DerivedClass.class
4). I'm not sure what you mean about even worse, but, unless you use the directory structure I described above, putting "C:\test" in the classpath will not solve the problem--although it would solve other problems you might encounter down the road.
 
Joyce Yu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer.
About questions 3, I still don't understand. You explain very clear, but it still doesn't work.
Here is my directory:
e:\test\core\java\demo\BaseClass.java
e:\test\core\java\demo\SamePackage.java

code in BaseClass.java:
package core.java.demo;
public class BaseClass
{
publicString publicMethod() { return "public Method"; }
protectedString protectedMethod() { return "protected Method"; }
privateString privateMethod() { return "private Method"; }
String packageMethod() { return "package Method"; }
}
code in SamePackage.java:
package core.java.demo;
public class SamePackage
{
BaseClass aBase;
public SamePackage(BaseClass aBase) {
this.aBase = aBase;
}
publicString accessBasePublic() { return aBase.publicMethod(); }
publicString accessBaseProtected(){ return aBase.protectedMethod(); }
publicString accessBasePrivate() { return aBase.privateMethod(); }
publicString accessBasePackage() { return aBase.packageMethod(); }
}

BaseClass.java is ok. When I compile SamePackage.java, it's supposed show me the errors about I access the private method in BaseClass.java, but in fact, it shows these errors:
SamePackage.java:7: cannot resolve symbol
symbol : class BaseClass
location: class core.java.demo.SamePackage
BaseClass aBase;
^
SamePackage.java:8: cannot resolve symbol
symbol : class BaseClass
location: class core.java.demo.SamePackage
public SamePackage(BaseClass aBase) {
^
2 errors

I don't know why it doesn't work as I thought.
Thanks in advance.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a situation where you would need to set the environment variable CLASSPATH. Does your CLASSPATH setting include e:\test\? Alternativly, you could issue the command:
javac -classpath e:\test SamePackage.java
(This will override the CLASSPATH environment setting, so if you have any other dependencies, list them along with the e:\test. Separate them by semicolons: e:\test;e:\otherclasses;e:\etc. Of course, if you don't need any other directories, you don't need to include them)
 
Joyce Yu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! It works!
 
reply
    Bookmark Topic Watch Topic
  • New Topic