Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Get the resource directory in AEM

Avatar

Avatar
Level 1
shekhart6405668
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
shekhart6405668
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile
shekhart6405668
Level 1

07-08-2019

Hi Guys,

I am using the AEM 6.2. and new to AEM. Can some one suggest me how to  get the real path of the resource directory? i.e "src/main/resources".

Replies

Avatar

Avatar
Give Back 50
Employee
Vish_dhaliwal
Employee

Likes

188 likes

Total Posts

356 posts

Correct Reply

123 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Vish_dhaliwal
Employee

Likes

188 likes

Total Posts

356 posts

Correct Reply

123 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Vish_dhaliwal
Employee

07-08-2019

Hello,

Not related to your question. Just want to let you know that you should start upgrading to AEM 6.5 since the core support of 6.2 has ended.

All Apps Help | Products and technical support periods

Regards,

Vishu

Avatar

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile
rampai
Level 3

08-08-2019

Hi shekhart64009905​,

Do you want to read a file included in resources section?

Avatar

Avatar
Validate 1
Level 1
mohitaem
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
mohitaem
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
mohitaem
Level 1

08-08-2019

Shekhar is trying to access "src/main/resources" folder from the java code. He want to create, write, read and delete the file in the resources folder. Since we install the java project as bundle its not possible to hardcode the path so he is finding it difficult to read the path of "src/main/resources".

Avatar

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile
rampai
Level 3

09-08-2019

If you want to manipulate the files using Java then you can use relative paths instead and take advantage of ClassLoader.

e.g. to read a file test.json inside test-folder under src/main/resources and get its contents in Java code:

InputStream is = getStreamFromResources("test-folder/test.json");

printFileContents(is);

If not inside a folder:

InputStream is = getStreamFromResources("test.json");

printFileContents(is);

Methods:

private static void printFileContents(InputStream inputStream) throws IOException {

  StringBuilder textBuilder = new StringBuilder();

   try (Reader reader = new BufferedReader(new InputStreamReader

  (inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {

   int c = 0;

   while ((c = reader.read()) != -1) {

  textBuilder.append((char) c);

  }

  }

   jsonString = new String(textBuilder);

   LOGGER.info(textBuilder.toString());

}

// get file contents as stream from classpath, resources folder
private InputStream getStreamFromResources(String fileName) {

  ClassLoader classLoader = getClass().getClassLoader();

  InputStream is = classLoader.getResourceAsStream(fileName);

   if (is == null) {

   throw new IllegalArgumentException("file is not found!");

  } else {

   return is;

  }

}

This is how my folder structure looks like:

1809960_pastedImage_6.png

Sample Output in HTL:

1809961_pastedImage_8.png

Regards,

Ram

Avatar

Avatar
Level 1
shekhart6405668
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
shekhart6405668
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
View profile
shekhart6405668
Level 1

09-08-2019

Hi Ram,

Thanks for response on this. Now file reading and writing inside test-folder make sense. But how can we listed all file inside the test-folder.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,122 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,122 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

09-08-2019

If you want to access the files in the JAR file during runtime, you can use the "regular" API you would always use in java, this is not specific to AEM. On the other hand, I doubt that the test classes are part of the resulting JAR file.