0

I've made a simple 'AntExecutor' app in eclipse that can run ant tasks programmatically and it works. But for university purposes I need to keep it independant from IDE. So, funnily, I'm strugling to create ant tasks which would compile,build my 'AntExecutor' app (which executes ant-tasks) :)

Stripped-down version I'm currently trying to define ant-tasks for only contains one source file in 'storageAccess' package:

./src/storageAccess/AntExecutor.java

I've got some libraries that AntExecutor.java makes use of at:

./lib

And the build file is at:

./build.xml

AntExecutor.java also needs ant libraries to execute ant tasks so they're added to CP at compile at. in build file:

<classpath path="${build};D:/DevTools/apache-ant-1.9.8/lib/;"/>

Full build.xml file:

<project name="AntExecutor" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build/classes/"/>
  <property name="dist"  location="build/jar/"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac  destdir="${build}">
      <src path="${src}"/>
      <classpath path="${build};D:/DevTools/apache-ant-1.9.8/lib/;"/>

    </javac>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into RunExecutor.jar file -->
  <jar destfile = "${dist}/RunExecutor.jar" basedir="${build}">

   <manifest>
      <attribute name = "Main-Class" value = "storageAccess.AntExecutor"/>
      <attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>
   </manifest>
  </jar>

  <copy todir="${dist}\lib">
   <fileset dir="lib"/>
  </copy>


  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

now if i run 'ant dist' command I get no errors, build succeeds and RunExecutor.jar file is created at ./build/jar

To check contents of RunExecutor.jar, I ran: jar tf build/jar/RunExecutor.jar

result:

META-INF/
META-INF/MANIFEST.MF
storageAccess/
storageAccess/AntExecutor.class

so it seems like storageAcces.AntExecutor class was indeed successfully compiled to .jar file.

However, if i try running it like this: java -jar build/jar/RunExecutor.jar

I get this error:

Error: Could not find or load main class storageAccess.AntExecutor

Main-question:

How come it can't find the class that is clearly in it.(as 'jar tf' shows) how do I fix this?

Also, what is the corret way to add ant/lib/*.jar files to CP for compiling and running 'RunExecutor.jar' ? is it okay just to specify the path to them as I do now? :

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>

or, maybe I should use wildcard like:

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/*.jar;"/>

or, should I frustratingly add all the files one by one?

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/ant.jar;"/> , etc...
EasternDude
  • 413
  • 4
  • 16
  • 2
    See: http://stackoverflow.com/questions/3143567/cannot-find-main-class-in-file-compiled-with-ant/3144290#3144290 – Mark O'Connor Mar 06 '17 at 00:28
  • After reading what Mark suggested, I redefined Class-Path to ant libs for my jar-file using property as suggested in that thread. That fixed the problem. However, did not bring any clarity on WHY it fixed the problem. I was not getting ` java.lang.NoClassDefFoundError` (as the person in Marks suggested thread). My Main-Class was 'not found' and Main method did not even run before. I can't understand how linking ant libraries in different way could have fixed the problem. – EasternDude Mar 06 '17 at 16:17

1 Answers1

0

The problem with

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>

is you are hard-coding path which is not a good practice. This jar will not execute on other machines as there are chances that they won't have lib under same location.

You can directly create executable jar from eclipse project itself. For steps refer this.

You can also put required lib in same jar file and they will by default get added to class-path.

Atul
  • 1,536
  • 3
  • 21
  • 37