如何使用在“Classpath”中添加* .jar文件的eclipse java项目创建jar文件

时间:2023-02-05 14:01:56

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. 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 Java项目,允许连接到数据库,给定数据库的URL和用于连接到该数据库的用户凭据。该项目在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文件,这样我就可以通过命令行运行我的Java项目,就像我在eclipse控制台上运行它一样。

I have tried [right click project ->export -> jar-> select all files on my project-> choose main method. Etc] to create the jar file. 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).

我试过[右键单击项目 - >导出 - > jar->选择我项目中的所有文件 - >选择main方法。等等]创建jar文件。我可以通过命令提示符(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? 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.

如何生成包含在其中的驱动程序的* .jar文件,以便我的程序在运行时实际可以找到它们?到目前为止,我将这些驱动程序放在项目根目录下的文件夹中,因为我认为这样他们将被打包。但是当我在终端中运行my.jar文件时,找不到它们。

1 个解决方案

#1


3  

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated Jar."

将项目导出为“Runnable”Jar文件(在Eclipse中右键单击项目文件夹,选择“Export ...”)。配置导出设置时,请务必选择“将所需的库提取到生成的Jar中”。

#1


3  

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated Jar."

将项目导出为“Runnable”Jar文件(在Eclipse中右键单击项目文件夹,选择“Export ...”)。配置导出设置时,请务必选择“将所需的库提取到生成的Jar中”。