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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:21:39+00:00 2026-06-03T01:21:39+00:00

I have a Swing Java program that reads *.txt files in a resource folder

  • 0

I have a Swing Java program that reads *.txt files in a resource folder called ‘res’ containing subfolders of different kind of JSONs. When I run directly from Eclipse, all is fine. If I create an executable jar it doesn’t work. I managed to make it work with an horrifying solution, putting all my text files in the package where they are called.

To read them I use this line of code:

InputStream is = this.getClass().getResourceAsStream(file + ".txt");

where file is a string. I thought this would search in the jar the specified file but obviously not. I tried to put the path to the files, res/subfolder/name_of_file.txt and subfolder/name_of_file.txt but to no avail.
Here is the structure of my project

Project
   src
      com.package
   res
      JSON_type1
      JSON_type2
         file.txt

From com.package, I have a class that must read from JSON_type2/file.txt. Besides the obvious reason to keep my text file organized in the subfolders is that my GUI will populates a drop down list with the content of those subfolders.

From what I’ve gather from other questions here, getResourceAsStream() would have been my solution if I wanted to keep my text files in the package where they are called. Other than that I found something about subclassing ClassLoader and overwriting findResource(). If possible I want to avoid this solution as I’m far from being familiar with that class and its functionalities.

Should you need more informations, let me know.

Thank you

P.S.: This question arose from my previous question here

  • 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-03T01:21:40+00:00Added an answer on June 3, 2026 at 1:21 am
    // added leading / to indicate search from root of class-path
    String file = "/res/JSON_type2/file.txt";
    InputStream is = this.getClass().getResourceAsStream(file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Swing Java application that performs searches, and the results are
I have a Java swing application with a panel that contains three JComboBoxe s
I have various Java Swing applications that are used by multiple users. My deployment
I have a Java Swing application that is being used as a cluster application.
I have a Java swing application with a button that produces a popup window
We have a client side application (Java/Swing) that we need an HTML rendering control
I have a swing Java application that preserves a lot of data (you can
I have a Java program that uses reflection to find and invoke the main(String[]
I have a fully-functional Java program that's fairly long, and I want to transfer
I am learning Java and have a rather simple program that returns a series

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.