Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with setting the classpath in ant

I'm having problems getting my Java program to run (it uses some third party JARs). I can compile it fine but when I call my run target in ant it says it can't find the class that I told it run in the classpath. Here's what my build.xml looks like:

<project basedir="." default="build">
<property name="build" value="build" />
<property name="src" value="." />
<property name="lib" value="lib" />

<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
    <fileset dir="${build}">
        <include name="*.class" />
    </fileset>
</path>

<target name="build">
    <javac srcdir="${src}" destdir="${build}">
        <classpath refid="classpath" />
    </javac>
</target>

<target name="run">
    <java classname="FirstClass">
        <classpath refid="classpath" />
    </java>
</target>

Does anyone know what I might be doing wrong?


Here's my stack trace from ant:

ant run Buildfile: build.xml

run:
[java] Could not find GuiStarter. Make sure you have it in your classpath
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:138)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:616)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:357)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:758)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:217)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
[java] Java Result: -1

BUILD SUCCESSFUL Total time: 1 second
like image 884
David Avatar asked Oct 15 '09 21:10

David


People also ask

How do I fix a CLASSPATH problem?

You have 3 solutions: add this class in the path of your other compiled classes (respecting the package naming of your directories) add the root directory of this class in your classpath (in your case "C:\java\project\") add this single class into a jar and add this jar to the classpath.

How do I know if my CLASSPATH is set correctly?

Select Start -> Control Panel -> System -> Advanced -> Environment Variables -> System Variables -> CLASSPATH. If the Classpath variable exists, prepend .;C:\introcs to the beginning of the CLASSPATH varible. If the CLASSPATH variable does not exist, select New.

What is CLASSPATH in ant?

Ant classpath: discussion dir defined before this code is run, and that variable points to your lib directory, which contains all of the jar files needed by your application. This code also creates a variable named class. path which can be referenced later in your Ant script using the variable syntax ${class. path} .


2 Answers

I think the problem is with your classpath path declaration. The build directory should be a <pathelement>

<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
    <pathelement location="${build}" />
</path>

Also, I would only include 3-rd party jars in your classpath refid. So the whole block looks like.

<path id="3rd-party-classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="build">
    <javac srcdir="${src}" destdir="${build}">
        <classpath refid="3rd-party-classpath" />
    </javac>
</target>

<target name="run">
    <java classname="FirstClass">
      <classpath>
        <pathelement location="${build}" />
        <path refid="3rd-party-classpath" />
      </classpath>
    </java>
</target>

Also, as DroidIn.net has pointed out, you should create a package for you program.

like image 118
Alexander Pogrebnyak Avatar answered Sep 18 '22 06:09

Alexander Pogrebnyak


The way it is - only jars inside lib directory relative to your path are getting loaded. Do you have all of your 3-rd party jars there? There may be some run-time libraries that you don't need at compile time but which are missing at run time. If you post your stacktrace I'll tell you more :)

like image 32
Bostone Avatar answered Sep 18 '22 06:09

Bostone