1

How to make Eclipse copy all the files (except .java) from the source directory to the output directory? I have some class-files under my sources, but they are filtered out. The setting "Preferences -> Java -> Compiler -> Building -> Filtered Resources" doesn't work for me.

Thanks!

EDIT:

I came out with a solution: add additional Builder to the Eclipse project. It's an Ant script:

<project name="example" default="copy_resources" basedir=".">
    <target name="copy_resources" depends="" >
        <copy todir="bin" overwrite="false">
            <fileset dir="src">
                <exclude name="**/*.java" />
            </fileset>
        </copy>
    </target>
</project>

However I have to manually trigger the "Build Project" command in order to Ant script be executed. When I do "Clean" it is not executed...

Andrej
  • 1,679
  • 1
  • 26
  • 40
  • How come you have .class file in your source directory? This looks pretty ugly... – Guillaume Polet Feb 27 '12 at 18:06
  • Please check this question for the background: http://stackoverflow.com/questions/9468607/how-to-make-javac-copy-existing-class-files-to-the-target-dir – Andrej Feb 27 '12 at 18:20

2 Answers2

2

You need to have class directories in a different directory than your source, I usually put them in a directory called "lib" (not just class directories but jars as well).

Once you have the classes in a different directory:

Right click on the project > Properties > Java Build Path > Libraries tab > Add Class Folder button > Select the directory > OK > OK

When you build your project the class directories should be included.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • It is different. ".class" files I have in my source are resources which I need to have at that place. – Andrej Feb 27 '12 at 18:17
  • Please check my another question for the background: http://stackoverflow.com/questions/9468607/how-to-make-javac-copy-existing-class-files-to-the-target-dir – Andrej Feb 27 '12 at 18:19
  • Why do you have to have them in the source directory? If you use the exact same package structure then it makes no difference where you place the class files (If you add them to the build path that is) – Nitzan Tomer Feb 27 '12 at 18:32
  • I access resources at runtime using getClass().getResource() of a class placed in the same package. This approach doesn't require absolute paths and works both from IDE and from JAR. – Andrej Feb 27 '12 at 18:41
  • May I ask why you are doing this? – Nitzan Tomer Feb 27 '12 at 19:00
  • Because resources are accessed using relative path via the helper class, they can be easily moved together with the helper class, it works both for running the program from IDE and JAR, it's not error-prone. – Andrej Feb 27 '12 at 19:17
  • No, I meant to ask why are you accessing class files at runtime as resources? – Nitzan Tomer Feb 27 '12 at 19:22
  • I have multiple libraries and I need to load only one of them at run-time, depending on the OS. I cannot put them as JAR files in my JAR. So I extracted them and put into source folder like resources. At run-time I add the URL of the required library to the class path. – Andrej Feb 27 '12 at 19:48
  • Yeah, as I thought, there's a better way to do that (in my opinion) and it's called "class folders". It's exactly like using jars but in their extracted way (so that you can include them in your jar). This can be done both with eclipse and ant (as you mentioned you want) and then there's no need to do any extra work at runtime, you just use the classes you need. – Nitzan Tomer Feb 27 '12 at 19:54
  • I have multiple variants of same library (for different OSs). I cannot load them all together because they have same class and package names. – Andrej Feb 28 '12 at 00:11
  • There are plenty of methods to do exactly this. Have you searched on the subject before you decided to take this approach? I highly recommend you to stop with your current approach and consider others, for example: http://stackoverflow.com/questions/7915315/how-can-i-package-a-java-desktop-application – Nitzan Tomer Feb 28 '12 at 07:38
  • Of course I did :-) Thank you for your advice, but I don't see any disadvantages which would be relevant for my situation. Probably I didn't explain my situation well to you. – Andrej Feb 28 '12 at 09:42
0

It sounds like the .class files are artifacts from something else, either checked in, or created by an external process. In either case, Eclipse appears to be blissfully unaware of them (from your description).

You can try the following: add a new builder to your project. Select "Ant" and choose the build.xml at the root of your workspace (or whatever location you decided - you need to make this file). The ant script will run whenever your project is rebuilt.

<?xml version="1.0" encoding="UTF-8"?>
<project default="copy">
  <target name="copy">
    <mkdir dir="someplace"/>
      <copy todir="someplace">
        <fileset dir="source">
          <include name="**/*.class"/>
        </fileset>
      </copy>       
   </target>
</project>

You just need to fixup the source and destination of the files.

Ken Brittain
  • 2,255
  • 17
  • 21
  • You got the problem correctly. I posted same solution one minute before you :-) Unfortunately I have to manually trigger the "Build Project" operation even if "Build automatically is enabled. – Andrej Feb 27 '12 at 18:58