Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:13:40+00:00 2026-06-13T19:13:40+00:00

By local filename I mean that resource file is located in the same directory

  • 0

By “local filename” I mean that resource file is located in the same directory as class file. In the case below this is JUnitRunner.class file. Java’s getResource() file can handle this if path does not start with /‘

I can’t figure out, how to do the same ClassPathXmlApplicationContext constructor?


package springtests;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JUnitRunner {

    private final static Logger log = LoggerFactory.getLogger(JUnitRunner.class);

    @Test
    public void test() throws URISyntaxException {

        String filename = "test01.xml";

        URL url = getClass().getResource(filename);


        File file = new File(url.toURI());
        log.info("File exists: {}", file.exists());

        try {
            new ClassPathXmlApplicationContext(filename);
        }
        catch(Exception e) {
            log.error("Can't load context", e);
        }

    }
}

the output follows

15:32:27,375 3    [main] INFO  springtests.JUnitRunner  - File exists: true
15:32:27,422 50   [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ee3aa7: startup date [Thu Nov 01 15:32:27 MSK 2012]; root of context hierarchy
15:32:27,475 103  [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [test01.xml]
15:32:27,477 105  [main] ERROR springtests.JUnitRunner  - Can't load context
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test01.xml]; nested exception is java.io.FileNotFoundException: class path resource [test01.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

...

UPDATE

XML file is located in the same folder as class file. It is visible from the fact that getResource() see it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:13:40+00:00Added an answer on June 13, 2026 at 7:13 pm

    so, your xml file in package springtests and correct creation of AppContext should be

    new ClassPathXmlApplicationContext("springtests/test01.xml");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I converted a local sound file to a resource, which built this in my
Given a local directory structure of /foo/bar , and assuming that a given path
save dialog saves file to the local machine. But after that, my page stand
I have a string that contains something like this: ##### abc 'foo' /path/to/filename:1 #####
Using local variables seems advisable in a partial that could be used application-wide to
My local copy works fine, then I publish the website and I get this
Are local object variables the variables that are used or initialized in a method,
I run a local directory website (think yelp/yell.com etc) and need to provide analytical
Currently, my script downloads .txt files from an FTP site into a local directory
Below code is for donwnloading file from azure blob. I have problem with .docx,.xlsx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.