简体   繁体   English

Java:将包含的Jar文件添加到Linux中创建的Jar文件中

[英]Java: Adding included Jar files to a created Jar file in linux

I need help including imported jar files into my java program in Linux. 我需要帮助,包括将导入的jar文件包含到Linux中的Java程序中。 Here is the program: 这是程序:

import java.sql.*;
public class CreateCoffees
{
    public static void main(String args[])
    {
        try {
             Class.forName("com.ibm.db2.jcc.DB2Driver"); 
        }
        catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage()); 
            System.exit(1);
         }
    }
}

In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); 为了执行Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath: 我需要将两个.jar文件添加到类路径中:

db2jcc_license_cu.jar
db2jcc4.jar

I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this: 我将这些jar文件放入与CreateCoffees.java文件相同的目录中,然后像这样编译并运行它:

javac CreateCoffees.java 
java CreateCoffees

But I got this error 但是我得到这个错误

ClassNotFoundException: com.ibm.db2.jcc.DB2Driver

Then I tried the "-classpath" option 然后我尝试了“ -classpath”选项

javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees

but got this 但是得到这个

Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees

How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ? 如何将那些jar文件包含到我的可运行jar中,以便可以使用java -jar myjar.jar来运行它?

Try this 尝试这个

java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees

when you use -classpath it looses current directory from classpath so it needs . 当您使用-classpath它会从classpath中删除当前目录,因此需要. in classpath as well explicitly 同样在classpath中

How to include the jars of your project into your runnable jar: 如何将项目的jar包含到可运行的jar中:

I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. 我将逐步介绍在Ubuntu 12.10上运行的Eclipse版本:3.7.2。 I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it. 我还将向您展示如何制作build.xml以便您可以从命令行执行ant jar并使用提取到其中的其他导入的jar创建jar。

Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you. 基本上,您要求Eclipse构造将您的库导入jar的build.xml。

  1. Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there. 启动Eclipse并创建一个新的Java项目,创建一个新的包“ mypackage”,并添加您的主类: Runner将此代码放入其中。

    在此处输入图片说明

  2. Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. 现在包括来自Oraclemysql-connector-java-5.1.28-bin.jar 它使我们能够编写Java以连接到MySQL数据库。 Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar. 通过右键单击项目->属性-> Java构建路径->添加外部Jar->选择mysql-connector-java-5.1.28-bin.jar来执行此操作。

  3. Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar. 在eclipse中运行该程序,它应该运行,并告诉您用户名/密码无效,这意味着已使用jar正确配置了Eclipse。

  4. In Eclipse go to File -> Export -> Java -> Runnable Jar File . 在Eclipse中去File - > Export - > Java - > Runnable Jar File You will see this dialog: 您将看到以下对话框:

    在此处输入图片说明

    Make sure to set up the 'save as ant script' checkbox. 确保设置“另存为蚂蚁脚本”复选框。 That is what makes it so you can use the commandline to do an ant jar later. 这就是它的原因,因此您以后可以使用命令行来做一个ant jar

  5. Then go to the terminal and look at the ant script: 然后转到终端并查看ant脚本:

    在此处输入图片说明

So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar . 因此,您看到了,我运行了jar并没有出错,因为它找到了嵌入在Hello.jar的包括的mysql-connector-java-5.1.28-bin.jar

Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class 在Hello.jar中查找: vi Hello.jar ,您将看到对com/mysql/jdbc/stuff.class许多引用

To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml , and change the target name from create_run_jar to jar . 要在命令行上执行ant jar以自动完成所有操作:将buildant.xml重命名为build.xml ,并将目标名称从create_run_jarjar

Then, from within MyProject you type ant jar and boom. 然后,从MyProject键入ant jar和boom。 You've got your jar inside MyProject. 您的Jar已放入MyProject中。 And you can invoke it using java -jar Hello.jar and it all works. 您可以使用java -jar Hello.jar调用它,并且一切正常。

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

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