0

I hope somebody can give me an advice...

Problem

In source directory of my project some packages contain resources which are not ".java"-files. Now I need to put there compiled ".class"-files. The problem is that ANT filters them out when I am building the final JAR. ".dll" and ".png" files are not filtered out.

How can I achieve that everything is copied to the target directory?

I use following Ant task:

<javac srcdir="${src}" destdir="${temp}" target="1.5" encoding="8859_1" debug="${compile.withdebug}" >
  <classpath refid="libs_path" />
</javac>

Background

I had to put in the final JAR multiple other OS-dependent JARs (SWT distributions). Only one JAR would be loaded at a program start. However it is not possible to let JVM load JAR from JAR without any special class loader. So I extracted all JARs to a package and JVM can load them now.

Why I want to put them under source (in Java package)? Because I want reference them relatively to the helper Java class:

org.example.swt_jars\
   swt_linux_32\
   swt_linux_64\
   swt_win_32\
   swt_win_64\
   SWTJarsResources.java

Thanks!

Andrej
  • 1,679
  • 1
  • 26
  • 40
  • You may want to look at `ant` `copy` task: http://ant.apache.org/manual/Tasks/copy.html – Aleks G Feb 27 '12 at 16:37
  • "Andrzej Doyle already answered the original question" – don't move the goalposts. If your question got answered correctly, accept the answer and make a new question. – millimoose Feb 27 '12 at 17:42
  • Ok. http://stackoverflow.com/questions/9469882/how-to-make-eclipse-compiler-copy-existing-class-files-to-the-target-dir – Andrej Feb 27 '12 at 18:05

1 Answers1

1

Javac is a compiler; getting it to move files around is using the wrong tool for the job.

I would suggest simply using an Ant copy task directly before or after the <javac> task to move existing class files across. Something like:

<copy todir="${temp}">
    <fileset dir="${src}">
        <include name="**/*.class" />
    </fileset>
</copy>

EDIT for Eclipse: what you're really trying to do here isn't have Eclipse copy the files, but for it to recognise that there are classes there that it needs to reference. So the simplest approach and one that I'd try first is to mark your src directory as a location that contains classes as well as one that contains sources.

I don't know if this would work for Eclipse - IDEA for example doesn't let a folder act as dual-purpose in this way. And again, it doesn't seem like it's quite the right tool for the job, as an IDE build is just collating the binaries it needs to run the app, and copying files around based on some project settings seems wrong somehow.

Ultimately I don't think your design is the cleanest, mixing classes in with source files is likely to be confusing. I appreciate that you're doing this because you want to use relative references, but perhaps you ought to abstract this out from the filesystem itself and use Classloader.findResource() (or probably getResourceAsStream()). This way you could arrange the files however you want, and so long as you run your application with both directories on the classpath, Java will be able to find the resource you're after. This will give you more flexibility in general, as well as solving this particular situation.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Now I have similar problem with Eclipse - it does not copy .class files when compiling sources... Do you mind if I reopen this Question? – Andrej Feb 27 '12 at 17:13
  • Thanks for your comments. I will give it a try. One more question: you wrote: "and so long as you run your application with both directories on the classpath". I need to integrate multiple swt_xxx.jar files in my final JAR. But at runtime I need only one of them being available in the class path. There are some solutions with custom class loaders but I find them too complex. – Andrej Feb 27 '12 at 18:34