将Java项目导出为.jar,其中包括.java源代码,并保留外部库依赖项

编程入门 行业动态 更新时间:2024-10-25 07:22:42
本文介绍了将Java项目导出为.jar,其中包括.java源代码,并保留外部库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如何将Java项目导出为也包括.java源代码的.jar(只需将其导出为纯.jar),以及如何将其导出为仅包含.class文件的可运行.jar(仅导出为.jar文件即可)选择可运行的.jar导出样式).如果该项目不依赖于外部库,我会注意到两个.jar都可以通过命令行正确运行.

I know how to export a Java Project as a .jar including also .java source code (just export it as a plain .jar), and how how to export it as a runnable .jar including only .class files (just select the runnable .jar export style). If the project doesn't have dependecies on external libraries, I noticed that both .jar run correctly by command line.

问题:如果项目包含对构建路径"中添加的某些外部库的依赖,则会导致导出为可运行的.jar可以正常运行,而普通的.jar则无法运行,因为从该库启动时找不到外部库命令行.

Problem: if the project contains dependecies on some external library added in the Build Path, it results that the .jar exported as runnable correctly works while a plain .jar doesn't because the external libraries are not anymore found when launching from command line.

注释:外部库以.jar的形式添加到项目中的"lib"文件夹中,并从那里添加到构建路径"中.

Annotation: external libraries are added as .jar in a folder "lib" in the project and from there added to the Build Path.

我希望能够导出Java项目,同时包括.java源代码并保留对外部库的依赖关系,以便在通过命令行启动它时可以运行它.有什么建议吗?我所能找到的只是将其导出为可运行的.jar",但这将隐藏.java源代码.

I want to be able to export the Java project while either including the .java source code and preserving the dependencies on external libraries so that it will run when launching it by command line. Any suggestion? All that I can find out is "just export it as runnable .jar", but this will hide the .java source codes.

编辑:有人可以解释为什么会发生这种情况吗?

Can anyone explain why this happens?

推荐答案

如果您不需要自动化的方法,则可以通过以下手动步骤来实现:

If you don't need an automated way you can achieve it with following manual steps:

  • 首先将其导出为可运行的jar(a.jar)
  • 第二次导出为带有源(b.jar)的jar
  • 使用7-zip或其他存档工具将源集成到可运行的jar中.打开两个jar并将包含源(包名称的第一部分)的文件夹从jar(b.jar)拖到可运行jar(a.jar)的z-zip窗口中

更多推荐

将Java项目导出为.jar,其中包括.java源代码,并保留外部库依赖项

本文发布于:2023-11-16 21:03:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源代码   项目   Java   java   jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!