可以“导出到 Eclipse 中的 JAR”和 javac 一样聪明吗?
我想将 Java 项目的一部分导出到 jar 文件中,同时只包含运行主类所需的类文件。该项目包含具有类似但不相同的依赖关系的其他主要类。我可以运行一个自定义脚本来实现它,如下所示:
mkdir temp
cd temp
javac -sourcepath "../" -d . ../MAINCLASS_A.java
jar cmf MAINCLASS_A.manifest ../MAINCLASS_A.jar *
为了优雅并更轻松地与其他人共享,我想直接从 Eclipse 运行它而不是这个自定义脚本。但是,当我导出到 JAR 文件或可运行 JAR 文件时,项目中的每个类文件都会包含在内。我知道我可以设置手动过滤器来排除某些文件,但似乎应该有一个更智能的选项。如何将 Eclipse 配置为仅包含主类实际依赖的类文件?
I want to export part of a Java project into a jar file while only including the class files that are necessary to run the main class. The project contains other main classes with similar but not identical dependencies. I can run a custom script to achieve it, like this:
mkdir temp
cd temp
javac -sourcepath "../" -d . ../MAINCLASS_A.java
jar cmf MAINCLASS_A.manifest ../MAINCLASS_A.jar *
For elegance and to more easily share with others I'd like to run it directly from Eclipse instead of this custom script. However, when I Export into a JAR file, or a Runnable JAR file, every class file in the project is included. I know I can set up manual filters to exclude certain files, but it seems there should be a smarter option. How can I configure Eclipse to only include the class files that the main class actually depends on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你想做的事情在一般情况下是不可能的。是的,理论上 javac 可以找到(并且找到)类之间的所有依赖关系。但只有当这些依赖项在您的项目中进行硬编码时,即您不直接或间接使用任何动态类加载和反射时,它才是正确的。
真实的应用程序通常使用各种第三方库、框架等。因此,即使您从未在自己的代码中说过
Class.forName()
,有时也会使用动态类加载。我认为如果您需要这种优化,您必须编写构建自定义 jar 的自定义脚本。顺便说一句,您需要它的事实可能会提示您您的项目组织不正确。您可能应该将您的项目分成 2 个或更多。每一个都应该只包含它的依赖项。在这种情况下,您可以使用内置的 Eclipse 功能来打包 jar 文件。I think that what you want to do is impossible in general case. Yes, theoretically javac can find (and finds) all dependencies between classes. But it is correct only if these dependencies are hard coded within your project, i.e. you do not use any dynamic class loading and reflection either directly or indirectly.
Real applications typically use various third party libraries, frameworks etc. So, sometimes dynamic class loading is used even if you never say in your own code
Class.forName()
. I think that if you need such kind of optimization you have to write your custom script that builds your custom jar. BTW the fact that you need it probably gives you a tip that your projects are organized incorrectly. You should probably separate your project into 2 or more. Each one should contain only its dependencies. In this case you can use the built in eclipse ability to package jar files.你不能那样做。主类所依赖的所有类直到运行时才会被知道。
假设我们正在使用 Class.forName("com.foo.Bar") 加载一个类。在这种情况下,主类中对
com.foo.Bar
的唯一引用是字符串文字,它不会被javac
或jar.
因此,您唯一的选择是设置手动过滤器,保存 .jardesc 文件并将其用作稍后创建类似 Jar 文件的脚本。
这应该会给你一个良好的开端。
http: //help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
You can't do that. All the classes on which your main class depends on will not be known until runtime.
Say, we're loading a class using
Class.forName("com.foo.Bar")
. In this case, the only reference tocom.foo.Bar
inside the main class is the string literal, which will not be loaded byjavac
orjar
.Thus, your only option is to set up manual filters, save the
.jardesc
file and use it as a script to create similar Jar files later on.This should give you a head start.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm