1

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.

ben
  • 5,671
  • 4
  • 27
  • 55
  • Does this program run ok outside of Ant? – tjg184 Oct 01 '14 at 16:26
  • If I use the `-cp CLASSPATH` option I can run it as long as it is not packed as a jar i.e. `java -cp CLASSPATH -jar myjar.jar` does not work. – ben Oct 01 '14 at 16:29
  • It looks like your classpath xml element may be outside of the java tag. – tjg184 Oct 01 '14 at 16:31
  • Woops you are right. Unfortunately I still get the error after correcting it. – ben Oct 01 '14 at 16:34
  • You might echo out lib_dir when you hit ant run. I'm running out of ideas here. Also, note if you use java -jar you still need to specify path of external jars if you're not doing that already. – tjg184 Oct 01 '14 at 16:39
  • Possibly related too if you're using a jdbc 3 driver: http://stackoverflow.com/questions/16696688/no-suitable-driver-found-for-jdbcpostgresql-192-168-1-85432-nexentasearch – tjg184 Oct 01 '14 at 16:40
  • Hm I'm using jdbc 4. I echoed out the classpath and it looks fine. – ben Oct 01 '14 at 16:52

1 Answers1

0

Why not try building the classpath as a then passing it to the java execution by reference? Do something like this inside your 'run' target:

<path id="runtime.classpath">
  <fileset dir="${lib_dir}">
    <include name="*.jar"/>
  </fileset>
  <fileset dir="${build_dir}">
    <include name="*.jar"/>
  </fileset>
</path>
<pathconvert property="classpathForPrinting" refid="runtime.classpath"/>
<echo>Classpath is ${classpathForPrinting}</echo>
<java fork="true" failonerror="true" classname="helloworld.Main">
    <classpath refid="runtime.classpath"/>
</java>
David
  • 2,602
  • 1
  • 18
  • 32
  • I'm currently using a similar approach. However my original intent was to specify a jar file in the java task. – ben Oct 03 '14 at 21:02