如何让 JavaC 将现有的类文件复制到目标目录? [英] How to make JavaC copy existing class files to the target dir?

查看:31
本文介绍了如何让 JavaC 将现有的类文件复制到目标目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人能给我建议...

I hope somebody can give me an advice...

问题

在我的项目的源目录中,一些包包含不是.java"文件的资源.现在我需要把编译好的.class"文件放在那里.问题是当我构建最终的 JAR 时,ANT 会将它们过滤掉..dll"和.png"文件不会被过滤掉.

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?

我使用以下 Ant 任务:

I use following Ant task:

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

背景

我不得不在最终的 JAR 中放入多个其他依赖于操作系统的 JAR(SWT 发行版).程序启动时只会加载一个 JAR.但是,如果没有任何特殊的类加载器,就不可能让 JVM 从 JAR 加载 JAR.所以我将所有 JAR 提取到一个包中,JVM 现在可以加载它们.

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.

为什么我要将它们放在源代码下(在 Java 包中)?因为我想相对于辅助 Java 类引用它们:

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

谢谢!

推荐答案

Javac 是一个编译器;让它移动文件是使用了错误的工具来完成这项工作.

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

我建议在之前或之后直接使用 Ant 复制任务 任务来移动现有的类文件.类似的东西:

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:您在这里真正想做的不是让 Eclipse 复制文件,而是让它认识到那里有需要引用的类.因此,最简单的方法也是我首先尝试的方法是将您的 src 目录标记为包含类以及包含源的位置.


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.

我不知道这是否适用于 Eclipse - 例如,IDEA 不会让文件夹以这种方式充当双重用途.再说一次,它似乎不是完成这项工作的合适工具,因为 IDE 构建只是整理运行应用程序所需的二进制文件,而根据某些项目设置复制文件似乎错误em> 不知何故.

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.

最终我认为您的设计不是最干净的,将类与源文件混合可能会造成混淆.我很感激你这样做是因为你想使用相对引用,但也许你应该从文件系统本身中抽象出来并使用 Classloader.findResource()(或者可能是 getResourceAsStream()).通过这种方式,您可以随意安排文件,只要您在类路径上的两个目录中运行您的应用程序,Java 就能够找到您所需要的资源.总的来说,这将为您提供更大的灵活性,以及​​解决这种特殊情况.

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.

这篇关于如何让 JavaC 将现有的类文件复制到目标目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆