Let's say that I have two classes (Bob and Tom) such that Bob uses Tom, but Tom does not require Bob. Both of these files are located 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 attempt to compile Bob in the typical fashion, I can get it to implicitly compile Tom.java
as well since it is recognized as a dependency of Bob.java
. That works great.
But now let's say that I want to place Tom in a JAR file. So I build Tom.java
and place the results into a JAR file: libTom.jar. I now compile with the classpath pointing at libTom.jar
. Unfortunately, the compiler sees the Tom.java
file and causes it to be compiled rather than using the class in libTom.jar
. Is there a way to get the compiler to skip the implicit construction of Tom.java
if the class is found in the classpath?
I recognize that this example is fairly contrived. Rest assured that there is a more complicated, less contrived use-case that surrounds this issue. Thanks.