Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell javac to ignore the lack of `import foo.Bar`?

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?

like image 365
uPaymeiFixit Avatar asked Mar 16 '23 02:03

uPaymeiFixit


2 Answers

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.

like image 85
M A Avatar answered Mar 24 '23 15:03

M A


No, there is no such option in javac command.

like image 43
Amit Bhati Avatar answered Mar 24 '23 15:03

Amit Bhati