如何将一个.jar依赖成生成我的应用我的最后.jar文件Ant目标? [英] How to include a .jar dependency into the ANT target that generate my final .jar file of my application?

查看:119
本文介绍了如何将一个.jar依赖成生成我的应用我的最后.jar文件Ant目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的蚂蚁和我有以下问题创建的build.xml 文件处理的的.jar 以下文件的创建单级应用:

I am pretty new in Ant and I have the following problem creating the build.xml file that handle the creation of a .jar file of the following single class 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的行为通过在连接康恩对象)。

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).

要获得它使用这个类的 oracle.jdbc.OracleDriver 的再present的Oracle数据库驱动程序的连接。这个类包含到一个名为jar文件的 ojdbc6.jar 是推在 LIB 我的项目文件夹内。

To obtain the connection it use this class oracle.jdbc.OracleDriver that represent the Oracle DB Driver. This class is contained into a jar file named ojdbc6.jar that is putted inside the lib folder of my project.

所以我有以下项目结构:

So I have the following project structure:

**edi-sta** (the project root)
     |
     |----> **lib**
     |         |
     |         |-------> **ojdbc6.jar**
     |
     |----> **src**
     |         |
     |         |-------> **Main.java**
     |
     |----> **build.xml**

所以我创造了这个的build.xml 文件:

<project name="edi-sta">

    <description>
        EDI-STA
    </description>

    <!-- ========================================================================= -->
    <!-- === Project Paths ======================================================= -->
    <!-- ========================================================================= -->

    <property name="project.base.dir" value="."/>

    <!-- ========================================================================= -->
    <!-- === DO NOT EDIT BELOW THIS LINE ========================================= -->
    <!-- ========================================================================= -->
    <!-- === Library Names ======================================================= -->
    <!-- ========================================================================= -->

    <property name="libname.ojdbc6" value="ojdbc6.jar"/>  <!-- JDBC DRIVER FOR ORACLE DB -->

    <!-- =================================================================== -->
    <!-- =========================== Classpath ============================= -->
    <!-- =================================================================== -->
    <fileset id="classpath.jars" dir="${project.base.dir}/lib">
        <include name="${libname.ojdbc6}" />
    </fileset>

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

    <target name="compile" depends="clean">
        <mkdir dir="build/classes"/>

        <echo>INTO compile TASK</echo>

        <javac srcdir="src" destdir="build/classes" />

    </target>

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

        </jar>
    </target>

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

</project>

问题是,在这个阶段,当我尝试执行我的 main.jar文件在控制台中我获得的 ClassNotFoundException的,以这种方式:

The problem is that, at this stage, when I try to execute my Main.jar in the console I obtain a ClassNotFoundException, in this way:

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> 

这是因为我不知道如何正确地包括在 ojdbc6.jar (包含Oracle驱动程序文件的 oracle.jdbc.OracleDriver )。

This happens because I don't know how correctly include the ojdbc6.jar (the file that contains the Oracle driver oracle.jdbc.OracleDriver).

所以我觉得我必须把它做进 JAR 的目标,所以我有prepared一个字段集 ID =classpath.jars 在我的推杆在 ojdbc6.jar

So I think that I have to do it into the jar target and so I have prepared a fieldset having id="classpath.jars" in which I putted the ojdbc6.jar.

但现在我有什么做的,用它来解决我的问题?我缺少什么?

But now what have I to do to use it to solve my problem? What am I missing?

TNX

推荐答案

基本上你问什么我怎么包中的一个jar文件到另一个,你不应该这样做(的原因有很多,其中之一是它是针对几乎所有许可证)。

Basically what you're asking is how do I package one jar file into another, you should not do this (for many reasons, one being it is against almost all licenses).

当你运行你编译当您运行,你需要在你的classpath的JDBC JAR:

When you run you compile AND when you run, you need the jdbc jar on your classpath:

与运行:

java -cp ./lib/ojdbc6.jar -jar Main.jar

之类的东西。

java -cp Main.jar:./lib/ojdbc6.jar Main

在你的build.xml运行目标还需要引用的类路径。

Your run target in your build.xml also needs to reference the classpath.

这篇关于如何将一个.jar依赖成生成我的应用我的最后.jar文件Ant目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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