我可以告诉javac忽略缺少

我可以告诉javac忽略缺少

本文介绍了我可以告诉javac忽略缺少`import foo.Bar`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用反射在运行时加载 MyClass.class (一个外部文件)。

I'm using reflection to load MyClass.class (an external file) at runtime.

MyClass.class 使用库 Bar 这意味着我需要将 import foo.Bar; 放在文件的顶部。

MyClass.class uses the library Bar, which would mean that I need to place import foo.Bar; at the top of the file.

不过, Bar 库已经加载到主类中,并加载了 MyClass

However, the Bar library is already loaded in the main class loading MyClass.

有没有办法让我告诉javac忽略 Bar 不存在,只是在没有它的情况下进行编译?

Is there a way for me to tell javac to ignore that Bar doesn't exist and just compile without it?

推荐答案

不可能。编译类时,编译器不会记忆已加载了哪些类(不要将其与与类加载有关的概念混淆-这是完全不同的故事)。每当编译一个类并且发现对该类的引用不在同一包中时,都需要一个 import 语句。

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.

这样说,您的问题似乎存在矛盾:根据您的说法, MyClass 已被编译,因为文件 MyClass.class 存在,因此这里没有涉及编译器。是由类加载器完成加载。在这种情况下,就类加载器而言,如果在主类中已经引用了 Bar ,则不会从 MyClass 。

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.

这篇关于我可以告诉javac忽略缺少`import foo.Bar`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:59