0

I am trying to load a XML file into a input stream with spring-mvc's ApplicationContext. But I am not able to read the file and I am getting conflicting information.

I am trying to load a xml file into a InputStream and find the file via:

ApplicationContext ctx =
  new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

I then what to initialize my InputStream from the ApplicantionContext but I am unclear how to. Right now I have:

private InputStream forms = getClass().getClassLoader().getResourceAsStream("/validation.xml");

However the issue at the moment is I cannot even find the file.

Conflicting information:

I am put the files under resources and again in webapp-> WEB-INF-> config to try to figure out where it could be loaded from.

ApplicationContext ctx =
    new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

Say FOUND!!!!!!!!!!!

public Validator() {

        this.testResource();

        if(ctx != null){
            System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+ctx); //this prints
        }else{
            System.out.println("NOT FOUND");
        }
    }

Output:

FOUND!!!!!!!!!!!!!!!!!!!!org.springframework.beans.factory.support.DefaultListableBeanFactory@635bc5cc: defining beans []; root of factory hierarchy

However this says the file cannot be found:

private void testResource(){

    Resource resource =
            this.ctx.getResource("classpath*:/validation.xml");

    try{
        InputStream is = resource.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();

    }catch(IOException e){
        System.out.println("ERROR it is not found: ");
        e.printStackTrace();
    }
}

--------------------------Update 1-----------------------

I now have the following and it too is giving me conflicting information saying found then an IOException:

@Configuration
public class Validator {

    private InputStream forms;
    private ValidatorResources resources;

    ApplicationContext ctx =
            new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

    private Resource resource = this.ctx.getResource("classpath*:validation.xml");

    public Validator() {

        if(resource != null){
            System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
        }else{
            System.out.println("NOT FOUND");
        }

        this.runInputStream();
    }

    private void runInputStream(){
        try{
            this.forms = this.resource.getInputStream();
            this.resources = new ValidatorResources(this.forms);

        }catch (IOException ex) {
            System.out.println("runInputStream IOException: "+ex);
        }catch (SAXException e) {
            System.out.println("runInputStream SAXException: "+e);
        }catch (Exception e){
            System.out.println("ERROR-------------"+e);
        }

    }
}

Output:

FOUND!!!!!!!!!!!!!!!!!!!!class path resource [classpath*:validation.xml] runInputStream IOException: java.io.FileNotFoundException: class path resource [classpath*:validation.xml] cannot be opened because it does not exist

Mike3355
  • 11,305
  • 24
  • 96
  • 184
  • Well, I don't know, but are you running a webapp? WEB-INF should be accessed through ServletContext? http://stackoverflow.com/questions/12883861/how-to-access-a-file-under-web-inf-folder-in-java-class. Can you put the file in src/main/resources, that's where I would expect access to any old file. – K.Nicholas May 19 '17 at 17:45
  • @KarlNicholas I have it in resources and in WEB-INF. I am trying to figure out why it won't find it. – Mike3355 May 19 '17 at 17:48

2 Answers2

0

Youd should remove the / :

 getClass().getClassLoader().getResourceAsStream("validation.xml");

And your validation.xml file has to be at the root of your resources folder.

If everything is fine, once your project is build you can find the validation.xml file in WEB-INF/classes, so it is available in the classloader.

Arnaud
  • 51
  • 3
  • I have it at the root of my resources file and in `webapp -> config ->` I am getting an error that says `ERROR-------------java.lang.IllegalArgumentException: Stream[0] is null` when I do `private InputStream forms = getClass().getClassLoader().getResourceAsStream("validation.xml");` – Mike3355 May 19 '17 at 15:29
0

I think you need to load the file as:

Resource resource =
    this.ctx.getResource("classpath*:validation.xml");

So we load the file assuming its anywhere on the classpath

Nitin Prabhu
  • 624
  • 6
  • 10
  • I do that and try the debugger says the class is null and when I print to the console: `System.out.println("RESOURCE: "+resource.getFile().getAbsolutePath());` ERROR: `IOException: java.io.FileNotFoundException` – Mike3355 May 19 '17 at 17:07