Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent implicit Java compilation when the class exists in the classpath?

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.

like image 485
Viper Bailey Avatar asked Jun 01 '12 20:06

Viper Bailey


People also ask

How do I compile a Java program with a specific version?

Yes, you can set the version of compiler at compile time. And compile your java code into old versions of java. Here we use javac to compile code that will run on a 1.4 VM. You might also need following parameter to set denote the version of your code.

What is runtime classpath in Java?

runtime classpath. Contains the classes that are used when your application is running. That's the classpath passed to the “java” executable. In the case of web apps this is your /lib folder, plus any other jars provided by the application server/servlet container.

When a referenced class can't be loaded by the JRE which exception is thrown by?

2. ClassNotFoundException. ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class.

How do I add a classpath to a class in Java?

You are required to include all the directories which contain . class and JAR files. PATH environment variable once set, cannot be overridden. The CLASSPATH environment variable can be overridden by using the command line option -cp or -CLASSPATH to both javac and java command.


2 Answers

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

like image 96
Hari Menon Avatar answered Oct 05 '22 22:10

Hari Menon


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

like image 44
Viper Bailey Avatar answered Oct 05 '22 23:10

Viper Bailey