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.
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.
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.
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.
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.
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 Tom
s 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
It appears that this just isn't possible, which is what I feared from the beginning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With