Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...