1

I have some data files in my WebApplication which I want to read.

How can I access them from an EJB?

I tried something like this but it didn't work:

@Singleton
public class Server {

  public void loadData() {
    InputStream is =
      this.getClass().getClassLoader().
      getResourceAsStream("WebContent/WEB-INF/Data/Data.xml");
    //read from is...
  }

}

Or is there any better way to handle read-only data files? I don't want to use a database, because I never have to write into these files, and I can parse big XML files very easily.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • possible duplicate of [Reading files from an EJB](http://stackoverflow.com/questions/10735221/reading-files-from-an-ejb) – Beryllium Sep 27 '13 at 07:18

2 Answers2

1

try:

public void loadData() {
  InputStream is = this.getClass().getClassLoader().getResourceAsStream("classpath:Data/Data.xml");
  //read from is...
}

More details here: URL to load resources from the classpath in Java

Community
  • 1
  • 1
JackDev
  • 11,003
  • 12
  • 51
  • 68
1

After some experimenting, I finally got it working. The Problem was the wrong Path. Right is:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("../Data/Data.xml");

(I don't know why)...

InputStream is = this.getClass().getClassLoader().getResourceAsStream("classpath:Data/Data.xml");

is not working for me but thanks for your help.