6

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.

user2428118
  • 7,935
  • 4
  • 45
  • 72
Viper Bailey
  • 11,518
  • 5
  • 22
  • 33
  • 3
    Although I haven't used it, have you tried the "-implicit" flag for javac? according to the help output: "-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files" which sounds like what you want. – Steve B. Jun 01 '12 at 20:26
  • why isn't that an answer? @SteveB. – 11684 Jun 01 '12 at 20:30

4 Answers4

1

If there are two classes with the same name and in the same package in the classpath, it is difficult to predict which one will get picked up when java compiles the code. In case the Tom class exists in the source itself, it will surely get picked up.

One way you can avoid it is to put one of the Toms in a different package. Another way is if you can move your current Tom to a separate project. I understand either of these might not be practical for you. If it is really necessary, you could experiment with writing your own ClassLoader. See this question for reference: Jar hell: how to use a classloader to replace one jar library version with another at runtime

Community
  • 1
  • 1
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
1

It appears that this just isn't possible, which is what I feared from the beginning.

Viper Bailey
  • 11,518
  • 5
  • 22
  • 33
0

Yes, you could do that by typing the full name of a class, i.e. Typing

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

11684
  • 7,356
  • 12
  • 48
  • 71
  • Sorry, I may have been a bit unclear on this element. The `Tom` classes in question are the exact same class. So the package would be the same package. I just want to avoid implicit compilation when no compilation would be necessary due to the fact that the class has already been compiled. – Viper Bailey Jun 01 '12 at 20:48
  • aha... You could place the could place the source code in another directory, and 'import' the class. – 11684 Jun 02 '12 at 19:43
  • or you might have a look at JRebel. @ViperBailey – 11684 Jun 02 '12 at 19:44
0

Can you split the java files into separate source directories? This would make sense as you could build your jar from the contents of one source dir, then just include the other source dir when compiling all the other set of files. This works well in Eclipse. Even better would be to use two projects in Eclipse.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
davidfrancis
  • 3,734
  • 2
  • 24
  • 21