How to make JavaC copy existing class files to the target dir?

匆匆过客 提交于 2019-12-25 17:18:15

问题


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!


回答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.



来源:https://stackoverflow.com/questions/9468607/how-to-make-javac-copy-existing-class-files-to-the-target-dir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!