0
votes

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

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

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**

So I created this build.xml file:

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

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> 

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

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

1
Please don't ask the same question twice. stackoverflow.com/questions/28458761/… Very annoyingMark O'Connor
Please stop asking the same question over and over again. The recommended way is to refine your current question.Mark O'Connor

1 Answers

1
votes

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

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

Run with:

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

or something like

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

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