-3

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.

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

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

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>

After that I have performed in order the clean, compile and jar targets I tried 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 statement:

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> 

But, as you can see, now happens a very strange thing, it seems that can't found the class that contains the Oracle driver (oracle.jdbc.OracleDriver) so the ClassNotFoundException is thrown.

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.

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

pnuts
  • 58,317
  • 11
  • 87
  • 139
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • First of all, don't use ANT. But if you absolutely insist, you need to add it to the &lt task. (Or you specify it on the command line). Dont forget to check the license situation if you include proprietary software. – eckes Feb 11 '15 at 19:57

1 Answers1

-1

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

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"/> 
                ....
ramp
  • 1,256
  • 8
  • 14
  • Now I will try. But what have I to put as value? The name of my jar or something like /lib/filename.jar ? – AndreaNobili Feb 11 '15 at 14:53
  • The manifest should contain the path of the referred-to jar file relative to the jar file that refers to it. So where is filename.jar relative to Main.jar ? – ramp Feb 12 '15 at 07:09