简体   繁体   English

如何从使用“类路径”中添加的 *.jar 文件的 eclipse java 项目创建 jar 文件

[英]How to create a jar file from an eclipse java project that uses *.jar files added in “Classpath”

I have an eclipse Java project that allows connection to a database, given the url of the database and user credentials for connecting to that database.我有一个 eclipse Java 项目,它允许连接到数据库,给定数据库的 url 和用于连接到该数据库的用户凭据。 The project runs on eclipse and successfully connects to databases with the help of "ojdbc14.jar" and "mysql-connector-java-5.1.38-bin.jar" drivers.该项目在 eclipse 上运行并在“ojdbc14.jar”和“mysql-connector-java-5.1.38-bin.jar”驱动程序的帮助下成功连接到数据库。

In eclipse I have added these two drivers to my project by [right clicking in my project]->run configuration-> [add those .jar drivers to my "Classpath], so that when I run my Java project, the drivers are loaded and everything runs smoothly.在 Eclipse 中,我通过[右键单击我的项目]-> 运行配置-> [将那些 .jar 驱动程序添加到我的“类路径],将这两个驱动程序添加到我的项目中,这样当我运行我的 Java 项目时,驱动程序被加载一切顺利。

I am trying to create a jar file that includes these two drivers within the created .jar file, so that I can run through the command line my Java project the same way I'm running it on the eclipse console.我正在尝试创建一个 jar 文件,在创建的 .jar 文件中包含这两个驱动程序,以便我可以像在 eclipse 控制台上运行它一样通过命令行运行我的 Java 项目。

I have tried [right click project ->export -> jar-> select all files on my project-> choose main method.我已经尝试过[右键单击项目-> 导出-> jar-> 选择我项目中的所有文件-> 选择主要方法。 Etc] to create the jar file.等]来创建jar文件。 I can open the generated jar through command prompt (Java -jar myjar.jar) and use the " main" method just like i do on eclipse but when it comes to load the driver the program stops because apparently I can't load the drivers (ojdb14.jar and mysql*.jar files).我可以通过命令提示符(Java -jar myjar.jar)打开生成的 jar 并使用“main”方法,就像我在 eclipse 上所做的那样,但是当加载驱动程序时程序停止了,因为显然我无法加载驱动程序(ojdb14.jar 和 mysql*.jar 文件)。

How can I generate a *.jar file with those drivers included in it so that my program actually can find them when it is run?如何生成包含这些驱动程序的 *.jar 文件,以便我的程序在运行时实际上可以找到它们? As of right now I have those drivers inside of a folder under the root of my project because I thought that that way they will be packed.截至目前,我将这些驱动程序放在项目根目录下的文件夹中,因为我认为这样它们会被打包。 But they can't be found when I run my.jar file in terminal.但是当我在终端中运行 my.jar 文件时找不到它们。

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export...").将您的项目导出为“可运行”Jar 文件(在 Eclipse 中右键单击项目文件夹,选择“导出...”)。 When you configure the export settings, be sure to select "Extract required libraries into generated Jar."配置导出设置时,请务必选择“将所需的库提取到生成的 Jar 中”。

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

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