How to prevent implicit Java compilation when a class exists in the classpath?

Say I have two classes (Bob and Tom) that Bob uses Tom, but Tom does not require Bob. Both of these files are in the same directory structure.

public class Bob {
  public static void main(String[] args) {
    System.out.println(Tom.MESSAGE);
  }
}

public class Tom {
  public static String MESSAGE = "Hello World";
}

If I try to compile Bob in a typical way, I can force him to implicitly compile Tom.java, since he is recognized as a dependency Bob.java. This works great.

But now say that I want to put Tom in a JAR file. Therefore, I build Tom.javaand put the results in a JAR file: libTom.jar. Now I am compiling with a classpath pointing to libTom.jar. Unfortunately, the compiler sees the file Tom.javaand forces it to compile, rather than using the class in libTom.jar. Is there a way to force the compiler to skip implicit building Tom.javaif the class is found in the classpath?

I understand that this example is pretty far-fetched. Be sure that there is a more complex, less far-fetched use case that surrounds this problem. Thank.

+5
source share
4 answers

It seems like it is simply impossible, which I was afraid from the very beginning.

0
source

, , , java . , Tom , , , .

- Tom . - Tom . , . , ClassLoader. . : Jar hell: jar

+1

, , , .. Typing

System.out.println (the.package.of.external.Tom.MESSAGE);
System.out.println (the.current.package.Tom.MESSAGE);

0
source

Can you split java files into separate source directories? That would make sense, since you could create your own jar from the contents of one source directory, and then just include a different source directory when compiling the whole other set of files. This works well in Eclipse. It would be even better to use two projects in Eclipse.

0
source

All Articles