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

2019-02-24 14:15发布

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?

2条回答
看我几分像从前
2楼-- · 2019-02-24 14:43

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.

查看更多
在下西门庆
3楼-- · 2019-02-24 14:50

No, there is no such option in javac command.

查看更多
登录 后发表回答