Just a concept that has eluded me... Anyone who can help make this understandable would be a God send.

in the directory c:/dev/myapp I have the subdir
src/com/mycompany/myapp/MyApp.java which consists of

package com.mycompany.myapp
 import com.mycompany.MyUtil
 public class MyApp {
   public static void main(String args[]) {
     new MyApp();
     MyUtil.Say();
   }
 }

src/com/mycompany/util/MyUtil.java which consists of

package com.mycompany.myutil
 public class MyUtil {
  public static void Say() {
    System.out.println("Hello");
  }
 }

If I go into the subdir src/com/mycompany/myapp and do a compile:
javac MyApp.java
It doesn't find MyUtil.

If i move the com/mycompany/util subdir into src/com/mycompany/
it does find it. It seems like a classpath issue, but I cannot seem to get my head around it.

My ant build seems to understand what is happening and runs things fine. Any enlightenment would be appreciated.

Recommended Answers

All 2 Replies

Spent a productive block of time with just the above code example and finagled it 2o different ways to Sunday, and I think I get it now. It's both classpath and package kind of deal. I think using Jars kind of obfuscated the knowledge for me and so when I tried doing things outside of the jar I was getting strange results.

Package names are interpreted starting from all the places in the classpath. Eg classpath begins with . (current dir), package is com.xxx.yyy - java looks for a dir com/xxx/yyy in the current dir. If it doesn't find that it does the same thing starting at the next location in the classpath etc.
Jars are treated exactly like directories.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.