1

I have a newbie Java question.

I had to make suite of J/DBUnit tests for some stored procedures we use in SQL Server. These tests use some XML files in a couple of sub-directories that I originally had placed in the same directory as my Java project.

Anyway, upon checking these tests in, our SVN manager wanted to keep the .java files in one part of the tree, and resources (like the XML files and required JARs) in another part of the tree.

So, my tests had originally referenced the XML files with a relative path which doesn't work now.

My question is:

  1. Can I make the directories with my XML files available with the CLASSPATH (I hope so).
  2. Assuming that works, how do I reference a file in my code that was included this way?
  3. If I shouldn't be using the CLASSPATH for this, I'm open to other solutions.

4 Answers 4

3

Forget calsspath. Provide your tests with a parameter/configuration which defines the root dir for the relative paths of the XML files.

Sign up to request clarification or add additional context in comments.

1 Comment

That works, but is ugly to handle if you start tests sometime with ant/maven, sometimes single ones with an IDE like Eclipse or Idea. No one likes to need to prepare a test setup before the run of a different test.
2

Using the classpath is no problem, the standard maven project layout looks like the following:

  • src
    • main
      • java
      • resources
    • test
      • java
      • resources
  • target
    • classes
    • test-classes

The compiler compiles src/main/java to target/classes, the resources of src/main/resources are copied to the target/classes folder, similar for the tests. If the tests have a classpath containing classes and test-classes, all works fine.

How is your project layout is, how is it build?

1 Comment

Well, it was just a small project essentially in a single folder, but the layout I'm moving to is Maven, so this answers the question well. I Just need to read up on Maven to understand how this works better I guess.
1

No, you should not use CLASSPATH in this instance since it is used by Java. However, you can use a similar approach by loading a value from an environment variable or configuration file which indicates the directory where the XML files are stored.

Comments

0

You can do this without making any changes to your classpath. The idea is to store the resource files in a separate directory, but have them copied to a directory in your classpath when you run your build process.

Here is an example configuration:

source Directory is ${basedir}/src/main/java

resource directory is ${basedir}/src/main/resources

In your build script, copy both the .java files and the resource files (.xml) to a directory in your classpath, say:

${basedir}/target/classes

Your test code runs against the target dir. The target directory is not checked in to SVN, keeping your SVN admin happy, and you don't have to make changes to your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.