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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:23:37+00:00 2026-05-12T14:23:37+00:00

I am trying to read a text file which is set in CLASSPATH system

  • 0

I am trying to read a text file which is set in CLASSPATH system variable. Not a user variable.

I am trying to get input stream to the file as below:

Place the directory of file (D:\myDir) in CLASSPATH and try below:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("/SomeTextFile.txt");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("//SomeTextFile.txt");

Place full path of file (D:\myDir\SomeTextFile.txt) in CLASSPATH and try the same above 3 lines of code.

But unfortunately NONE of them are working and I am always getting null into my InputStream in.

  • 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-05-12T14:23:37+00:00Added an answer on May 12, 2026 at 2:23 pm

    With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of:

    // From ClassLoader, all paths are "absolute" already - there's no context
    // from which they could be relative. Therefore you don't need a leading slash.
    InputStream in = this.getClass().getClassLoader()
                                    .getResourceAsStream("SomeTextFile.txt");
    // From Class, the path is relative to the package of the class unless
    // you include a leading slash, so if you don't want to use the current
    // package, include a slash like this:
    InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
    

    If those aren’t working, that suggests something else is wrong.

    So for example, take this code:

    package dummy;
    
    import java.io.*;
    
    public class Test
    {
        public static void main(String[] args)
        {
            InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
            System.out.println(stream != null);
            stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
            System.out.println(stream != null);
        }
    }
    

    And this directory structure:

    code
        dummy
              Test.class
    txt
        SomeTextFile.txt
    

    And then (using the Unix path separator as I’m on a Linux box):

    java -classpath code:txt dummy.Test
    

    Results:

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

Sidebar

Related Questions

I am trying to write some simple code which will read a text file
I am trying to read in a text input file that contains a list
Am trying to get values from a text file in which entries are delimited
Here I'm trying to read a text file which contains only integers.in every line.For
I am trying to read a text file in MATLAB which has a format
I'm trying to read a text file using a synchronous request. It doesn't work
I am trying to read a text file from raw folder, and it works
I am trying to read a text file line by line, and the text
Im having an issue when im trying to read in a text file while
I'm trying to read values from a text file into a hashtable, I want

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.