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

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

问题描述

我希望有人可以给我一个建议...

I hope somebody can give me an advice...

问题

在我的项目的源目录中一些包包含哪些不是的.java-files资源。现在我需要把那里编译的.class-files。问题是,ANT过滤出来,当我建立最终的JAR。 .DLL和巴纽文件不会被过滤掉。

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任务:

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

背景

我不得不把最终JAR其他多个操作系统相关的JAR(SWT分布)。只有一个JAR将在程序开始加载。然而这是不可能让从JAR JVM的负载的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 复制任务直接之前或之后的建议&LT;&javac的GT; 任务在整个移动现有的类文件。是这样的:

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>


修改作为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构建刚刚整理它需要运行的应用程序的二进制文件,以及将文件复制周围基于一些项目设置似乎的错误弄好了。

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.

最后,我不认为你的设计是最干净,混合类与源文件很可能是混乱。我AP preciate,你这样做是因为要使用相对引用,但也许你应该抽象这一点从文件系统本身和使用<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#findResource%28java.lang.String%29\"相对=nofollow> 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天全站免登陆