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 8463467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:27:45+00:00 2026-06-10T14:27:45+00:00

I have a unit test package in which I keep a few txt files.

  • 0

I have a unit test package in which I keep a few txt files. These get loaded via getClass().getResource(file); call and it works just fine. I added into the same folder a csv file, and if I supply its name as the parameter i.e. getClass().getResource("csvFile.csv"); I get null… any ideas why?

  • 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-10T14:27:47+00:00Added an answer on June 10, 2026 at 2:27 pm

    When you use

     getClass().getResource("csvFile.csv");
    

    it looks relative to the class.

    When you use

     getClass().getClassLoader().getResource("csvFile.csv");
    

    it looks in the top level directories of your class path.

    I suspect you want the second form.

    From Class.getResource(String)

    Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

    • If the name begins with a ‘/’ (‘\u002f’), then the absolute name of the resource is the portion of the name following the ‘/’.
    • Otherwise, the absolute name is of the following form:

      modified_package_name/name

      Where the modified_package_name is the package name of this object with ‘/’ substituted for ‘.’ (‘\u002e’).

    As you can see the directory translation of the package name of the class is used.


    For example, I have a maven project where the code is under src/main/java. My resources directory src/main/resources

    I add csvFile.csv to my resources directory which will be copied to my class path.

    public class B {
        B() {
            URL resource = getClass().getClassLoader().getResource("csvFile.csv");
            System.out.println("Found "+resource);
        }
        public static void main(String... args) {
            new B();
        }
    }
    

    which prints

    Found file:/C:/untitled/target/classes/csvFile.csv
    

    This is in the area built by maven from the resources directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Action class which I want to unit test: package com.gam.action.test;
I have a number of test suites that make use of the Test::Unit::TestCase package
I have a unit test that fails because headers are already sent. However, the
I have a unit test that I am writing and have run into an
I have a unit test that creates a mock calls my method to be
I have a unit test where I run a method for every item in
I have a unit test called TestMakeAValidCall() . It tests my phone app making
I have a unit test that tests if an Exception is throw, but this
I have a unit test project based on UnitTest++. I usually put a breakpoint
I have a Unit test project for my Application using DUnit framework. This project

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.