I'm using reflection to load MyClass.class
(an external file) at runtime.
MyClass.class
uses the library Bar
, which would mean that I need to place import foo.Bar;
at the top of the file.
However, the Bar
library is already loaded in the main class loading MyClass
.
Is there a way for me to tell javac to ignore that Bar
doesn't exist and just compile without it?
No this is not possible. When compiling a class, the compiler has no "memory" of which classes were already "loaded" (don't confuse this with the concept related to classloading -- that's a completely different story). Whenever a class is compiled and a reference to a class is found that is not in the same package, an import
statement is required.
This being said, it seems there is a contradiction in your question: from what you say, MyClass
is already compiled because the file MyClass.class
exists, so there is no compiler being involved here. It's the classloader that does the loading. In this case, as far as the classloader is concerned, if Bar
was already referenced in the main class, then it won't be loaded again from within MyClass
.
No, there is no such option in javac command.
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