是否可以在不提供依赖项的情况下编译 java 文件?
有一个java文件,其中有一些依赖jar。但现在,我没有这些 jar,我必须将其编译为 .class 文件。
可以这样做吗?
更新
感谢您的回答。
起初,我认为我们可以为缺少的依赖项创建一些存根,这很简单但很无聊。既然我们可以在不丢失存根的情况下创建存根以使编译器满意,为什么我们不能让工具自动执行此操作呢?该工具不需要创建存根,而是读取 java 文件,收集信息,然后构建 .class 文件。
但是如果java文件中的“import”语句包含“*”,那就会出现问题:
import aaa.*
import bbb.*
public class Hello {
World world;
}
我们不知道“World”类是在“aaa”包还是“bbb”包下。如果我们不熟悉缺失的依赖项,我们甚至不知道如何为“World”类创建一个存根。
但如果“导入”语句明确的话,我认为这是可能的,但也许没有人会写这样一个工具
There is a java file, which has some dependencies jars. But now, I don't have those jars, and I have to compile it to a .class file.
Is it possible to do this?
UPDATE
Thanks for your answers.
At first, I thought we can create some stubs for the missing dependencies, that's easy but boring. Since we can create the stubs without missing stubs to make the compiler happy, why can't we make a tool do it automatically? The tool doesn't need to create stubs, but reads the java file, collects informations, and then builds the .class files.
But if the "import" statements in the java file contain "*", that will be a problem:
import aaa.*
import bbb.*
public class Hello {
World world;
}
We don't know if the class "World" is under package "aaa" or "bbb". If we are not familiar with the missing dependencies, we even don't know how to create a stub for the class "World".
But if the "import" statements are clear, I think it's possible, but maybe no one will write such a tool
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以疯狂地将所需的依赖项手工制作为存根,除了让编译器满意之外什么也不做。
You could go crazy and hand craft the required dependencies as stubs that do nothing except keep the compiler happy.
不,抱歉。您需要类路径中的所有依赖项才能编译。
No. Sorry. You'll need all dependncies in the classpath to compile.
不可以。但是,如果您尝试编译的代码只是直接使用少数几个类,那么您可以提供依赖项类文件的删除版本。
然后理论上,如果您获取编译的 .class 文件并将真正的依赖项放置在类路径上,您的应用程序将使用正确的(非存根)依赖项类来工作。
No. But you could provide stubbed-out versions of the dependency class files, if it is only a handful of classes that the code your are trying to compile uses directly.
Then in theory if you take the .class file that compiles and place the real dependencies on the classpath with it your app will work using the correct (non-stubbed-out) dependency classes.
在编译任何文件之前,它总是查找任何依赖项。
但你说你没有那些罐子!
看看是否可以删除该项目/文件的依赖关系,然后尝试编译它。尝试一下!
Before any file is compiled it always looks up for any dependencies.
but you said you dont have those jars!!!
see if you can remove the dependencies relation for that project/file and then try to compile it. give it a try!