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

与世无争的帅哥 提交于 2019-12-31 00:43:51

问题


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?


回答1:


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.




回答2:


No, there is no such option in javac command.



来源:https://stackoverflow.com/questions/31973367/can-i-tell-javac-to-ignore-the-lack-of-import-foo-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!