My project layout:
.
├── bin
│ ├── build
│ │ └── hello.jar
│ │── classes
│ └── helloworld
│ └── Main.class
├── build.xml
├── lib
│ └── postgresql.jar
└── src
└── helloworld
└── Main.java
build.xml
<project>
<property name="class_dir" value="bin/classes"/>
<property name="src_dir" value="src"/>
<property name="build_dir" value="bin/build"/>
<property name="jar_name" value="hello.jar"/>
<property name="lib_dir" value="lib"/>
<target name="clean">
<delete dir="${class_dir}"/>
<delete dir="${build_dir}"/>
</target>
<target name="compile">
<mkdir dir="${class_dir}"/>
<javac includeantruntime="false" srcdir="${src_dir}" destdir="${class_dir}"/>
</target>
<target name="jar">
<mkdir dir="${build_dir}"/>
<jar destfile="${build_dir}/${jar_name}" basedir="${class_dir}">
<manifest>
<attribute name="Main-Class" value="helloworld.Main"/>
</manifest>
</jar>
</target>
<target name="run" >
<java jar="${build_dir}/${jar_name}" fork="true" failonerror="true">
<classpath>
<pathelement location="${lib_dir}/postgresql.jar"/>
</classpath>
</java>
</target>
The error when executing ant run
:
run:
[java] java.lang.ClassNotFoundException: org.postgresql.Driver
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] at java.lang.Class.forName0(Native Method)
[java] at java.lang.Class.forName(Class.java:259)
[java] at helloworld.Main.main(Unknown Source)
[java] Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5430/hello_world
[java] at java.sql.DriverManager.getConnection(DriverManager.java:689)
[java] at java.sql.DriverManager.getConnection(DriverManager.java:247)
[java] at helloworld.Main.main(Unknown Source)
Is it even possible to specify a classpath like this when executing a jar file? (I had the classpath echoed and it looked fine.)
EDIT:
According to http://www.coderanch.com/t/108841/tools/Ant-Compile-time-runtime-classpath what I'm trying to achieve is not possible.