如何正确导入JAR依赖成蚂蚁生成的.jar文件? [英] How to import properly a jar dependency into a .jar file generated by Ant?

查看:505
本文介绍了如何正确导入JAR依赖成蚂蚁生成的.jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的 Ant的,我有以下问题试图创建一个的build.xml 文件编译一个类(包含< STRONG>的main()方式)命令行应用程序。

I am pretty new in Ant and I have the following problem trying to create a build.xml file to compile a single class (that contains the main() method) command line application.

所以这是的code中的主要类(此时它是应用程序的唯一类):

So this is the code of the Main class (at this time it is the only class in the application):

import java.sql.*;
import java.util.TimeZone;


public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World !!!");

        System.out.println(args.length);

        if(args.length != 0) {
            String partitaIVA = args[0];
            String nomePDF = args[1];
        }

        Connection conn = null;
        Statement  stmt = null;

        try {
            Class.forName ("oracle.jdbc.OracleDriver");

            TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT+2");
            TimeZone.setDefault(timeZone);

            // Step 1: Allocate a database "Connection" object
            conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd");    // Oracle DB driver 

            System.out.println("After obtained connection with DB");

        } catch(SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

正如你所看到的行为在的main()的方法很简单,在这个时候只能获得与的Oracle 数据库(重presented连接通过在连接康恩对象)。它运行到IDE中(我用的的IntelliJ),它工作正常(我可以使用调试器看到它​​时,**连接康恩已正确设置好的)。

As you can see the behavior of the main() method is very simple, at this time only obtain a connection with an Oracle database (represented by the Connection conn object). Running it into the IDE (I am using IntelliJ) it works fine (I can see it using the debugger, the **Connection conn is correctly setted).

好了,现在我的工作在以下的的build.xml 文件中的蚂蚁编辑:

Ok, now I am working on the following build.xml file for the Ant compilation:

<project name="edi-sta">

    <description>
        EDI-STA
    </description>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/Main.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="Main"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/Main.jar" fork="true"/>
    </target>

</project>

好吧,之后我曾为了执行清洁 编译 JAR 目标,我tryied打开控制台,进入建立/罐/ 目录中包含的 main.jar文件文件,我试图执行它执行下列说明书:

Ok, after that I have performed in order the clean, compile and jar targets I tryied to open the console, access to the build/jar/ directory that contains the Main.jar file and I try to execute it performing the following statment:

C:\Projects\edi-sta\build\jar>java -jar Main.jar
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Main.main(Unknown Source)

C:\Projects\edi-sta\build\jar> 

但是,正如你所看到的,现在发生在一个很奇怪的东西,似乎无法找到包含Oracle驱动程序的类( oracle.jdbc.OracleDriver ),所以在 ClassNotFoundException的被抛出。

我认为,这是因为如果我打开(用WinZip)我产生的 main.jar文件文件只包含 Main.class 文件和 META -INF 文件夹(只包含MANIFEST.MF文件),但我没有在 ojdbc6.jar 文件,它包含我用的是Oracle驱动程序。

I think that this happens because if I open (with winzip) my generated Main.jar file it only contains the Main.class file and the META-INF folder (that contains only the MANIFEST.MF file) but I have not the ojdbc6.jar file that contains the Oracle driver that I use.

所以我的问题是:你有什么我做的包括此 ojdbc6.jar 依赖于正确生成我的 main.jar文件文件,避免在 ClassNotFoundException的

So my question is: what have I to do to include this ojdbc6.jar dependency properly in my generated Main.jar file and avoid the ClassNotFoundException?

TNX

推荐答案

您可以在JAR的manifest文件中的Class-Path属性定义的jar依赖。在这里阅读文档 - http://docs.oracle.com/javase/教程/部署/ JAR / downman.html

You can define jar dependencies in the Class-Path attribute of the manifest file of the jar. Read the documentation here - http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

要实现从你的ant任务,使用Class-Path属性如下

To achieve that from your ant task, use the Class-Path attribute as below

  <jar destfile="build/jar/Main.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="Main"/>
                <attribute name="Class-Path" value="your-jar-file"/> 
                ....

这篇关于如何正确导入JAR依赖成蚂蚁生成的.jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆