简体   繁体   English

通过命令行创建可运行的.jar时,添加其他.jar库

[英]Add additional .jar libraries when creating a runnable .jar via command line

Am working to reproduce the "export runnable .jar file" functionality of Eclipse with command-line calls directly from the java program itself. 我正在使用直接来自java程序本身的命令行调用来重现Eclipse的“export runnable .jar file”功能。 So those commands shall execute after Eclipse has compiled the project. 因此,这些命令应在Eclipse编译项目后执行。 I need this for better control what files are ending up in the .jar and to speed up the development process. 我需要这个来更好地控制文件在.jar中的结果并加快开发过程。

The following command successfully packs the structure and classes from the "bin" directory and also creates a manifest.mf file with the correct main class so the myjar.jar is runnable. 以下命令成功打包“bin”目录中的结构和类,并使用正确的主类创建manifest.mf文件,以便myjar.jar可运行。

ProcessBuilder builder = new ProcessBuilder("jar", "cvfe", "myjar.jar", "main.MainControler", "-C", "bin", "/");

Process process = builder.start();

However my project has several libraries (.jar files) added to the classpath via the class-path dialogues of Eclipse. 但是我的项目有几个库(.jar文件)通过Eclipse的类路径对话添加到类路径中。 For example I'm using the pi4j library. 例如,我正在使用pi4j库。 I expected that that its internal directory and class structure (something like "com.pi4j.***.class") would be added to the "bin" directory when eclipse compiles and executes the program. 我预计当eclipse编译并执行程序时,它的内部目录和类结构(类似“com.pi4j。***。class”)将被添加到“bin”目录中。 However it is not there and therefore that never ends up in the "myjar.jar" either (and I get class-missing errors when running the .jar). 然而它并不存在,因此永远不会在“myjar.jar”中结束(并且在运行.jar时我会遇到类缺失错误)。

When I use Eclipse' export function, the files are there though. 当我使用Eclipse的导出功能时,文件就在那里。

What do I need to do to have the .class files from the additional .jar libraries exported so they can be added? 如何从其他.jar库中导出.class文件以便添加它们,我需要做什么?

Huge thanks in advance :) 非常感谢提前:)

Your problem statement is not very clear and gives me the impression that you are trying to do something that should not be done. 您的问题陈述不是很清楚,并给我的印象是您正在尝试做一些不应该做的事情。

First of all: if you don't want to use Eclipse to build the project then avoid using it to perform the first part of the work (ie compilation): you can perform the whole process from command line and this will give you even more control on the build process. 首先:如果您不想使用Eclipse构建项目,那么请避免使用它来执行工作的第一部分(即编译):您可以从命令行执行整个过程,这将为您提供更多控制构建过程。

Second suggestion: do not reinvent the wheel. 第二个建议:不要重新发明轮子。 If you want to build from command line you can use Ant or Maven . 如果要从命令行构建,可以使用AntMaven In particular you can use Eclipse to generate an Ant build script that performs like "export runnable .jar file" then you can customize that script as you want: in Eclipse right-click on the project then select 特别是您可以使用Eclipse生成一个Ant构建脚本,其执行类似于“export runnable .jar file”,然后您可以根据需要自定义该脚本:在Eclipse中右键单击该项目,然后选择

Export / General / Ant Buildfiles 导出/常规/ Ant构建文件

and you'll get a build.xml script to be used with Ant. 你会得到一个与Ant一起使用的build.xml脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM