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