1

I am creating a Maven web application which stores many policy files in policy folder. My source folder layout looks like : enter image description here I tried to set the folder path as policyLocation as shown below:

try {
    // InputStream aa =
    // Accesscontrol.class.getClass().getResource(aa);
    String AbsolutePath = new File("/").getAbsolutePath();
    String policyLocation = (new File("."))
            .getCanonicalPath() + File.separator + "policy";
    System.setProperty(
            FileBasedPolicyFinderModule.POLICY_DIR_PROPERTY,
            policyLocation);
} catch (IOException e) {
    System.err.println("Can not locate policy repository");
}

But this is returning my Eclipse folder. Is there anyway I can get the policy folder that is not inside Resource folder (cause it can't be included inside Resource) in java web application?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Milson
  • 1,525
  • 3
  • 15
  • 29
  • That kind of resource should normally be located in `src/main/resources` according to Maven and Java standards. Why isn't yours? – E-Riz Jan 22 '16 at 15:35
  • so can you give me code sample for setting the policy folder inside resources folder as policyLocation variable @E-Riz – Milson Jan 22 '16 at 15:37
  • I'm not sure what you're trying to do, but in a webapp you can't rely on the file system being present or accessible. Most web apps are deployed as WARs, not folders and files. – E-Riz Jan 22 '16 at 15:40
  • All resources should be placed under `src/main/resources`. Then you access it with `SomeClass.class.getResourceAsStream` and you read from the stream. – Tunaki Jan 22 '16 at 15:48
  • @Tunaki are you sure this will hold for folder also cause I want only folder path not file path – Milson Jan 22 '16 at 15:54

1 Answers1

0

Please keep in mind that your application has more than 1 way to run:

  • from within IDE (Eclipse) where certain things are done automatically for you (like compilation on the fly)
  • when you build your web application using maven, do you build it as jar, war, or perhaps ear ? Once built, how do you deploy and run it?

Firstly you have to ensure that your build process (maven) includes the policy files in the distributable (e.g. in the war file) in a known location which you can reference. Then you can modify the classpath within the jar or war and to ensure that your policy files are on the classpath.

With such setup all you have to do is to address it as a resource as per this answer

Community
  • 1
  • 1
diginoise
  • 7,352
  • 2
  • 31
  • 39