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

  • Home
  • SEARCH
  • 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 9225133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:13+00:00 2026-06-18T04:30:13+00:00

In Java, I have tried the different techniques of reading a file inside the

  • 0

In Java, I have tried the different techniques of reading a file inside the jar. I have gotten one to work inside another program, but not this one. The InputStream that I get from the classname.class.getClassLoader().getResourceAsStream(filenamestring); method returns null.

 package mainpkg;

 import java.awt.GridLayout;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JTextField;
 import javax.swing.UIManager;

 public class MainClass extends JFrame{
JTextField jtf = new JTextField();
boolean isInstalling = false;
public MainClass(){
    super("StringThatTitleWillBe");
}
public void initiate(){
    setLookAndFeel();
    setSize(550, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout bl = new GridLayout(2, 1, 10, 10);
    setLayout(bl);

    JButton b = new JButton("Install");
    b.addMouseListener(new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent arg0) {
            if(!isInstalling){
            isInstalling = true;
            install();
            }
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {

        }

        @Override
        public void mouseExited(MouseEvent arg0) {

        }

        @Override
        public void mousePressed(MouseEvent arg0) {

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {

        }

    });
    add(b);
    jtf.setEditable(false);
    add(jtf);
    //show
    setVisible(true);
}
private void setLookAndFeel() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception exc) {
        System.out.print("failed to load look and feel");
    }
}
public static void main(String[] args){
    MainClass m = new MainClass();
    m.initiate();
}

public void install() {
    String files = "name of jarfile inside the jar to be read";
    InputStream stream = MainClass.class.getClassLoader().getResourceAsStream(files);
    File f = new File(files);
    if(stream == null)
        System.out.println("stream is null");
    try {
        int val = stream.read();
        BufferedWriter br = new BufferedWriter(new FileWriter(f));
        while(val != -1){
        br.write(val);
        val = stream.read();
        }
        stream.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    isInstalling = false;
     }
 }

It Prints “stream is null” and gives an error on the line: stream.read();
I know that means that the file is not being converted to an InputStream properly.
Have I made a stupid mistake??? please help. Thanks in advance
Edit: the file that I want to copy in the same directory as the jar is directly inside the jar. It is not in a package or any such additional directory.

  • 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-18T04:30:14+00:00Added an answer on June 18, 2026 at 4:30 am

    If a getResourceAsStream(String) returns null, that means that the class loader can’t find a resource with the name you gave it. Either it is plain wrong, or maybe you have used a relative name and the resource isn’t in the “/” directory in the JAR.

    For details, read the javadoc for ClassLoader.getResourceAsStream, paying careful attention to:

    • the value returned, and
    • the handling of relative pathnames (follow the getResource link!!).

    If you want to load a resource using a relative name that is resolved relative the classes package, you should write this:

        MainClass.class.getResourceAsStream(files);
    

    The Class.getResource... methods resolve relative names relative to the class. So the above would resolve "foo.txt" to "/mainpkg/foo.txt" … where your version would resolve it to "/foo.txt".

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

Sidebar

Related Questions

I'm looking for some explanation for Java regex. I have read and tried different
I have tried different ways to get the row count in java JDBC, nut
How do I create a Java package for different files? I have tried the
I need to monitor console output in Java, I have tried several different ways
I have a java application (jar file) and I want to add some sort
I have tried several different examples but I cannot get any video to show.
I have tried different ways to write a string to a file. File file
I have tried several ways to login to a website through java. I have
We have tried to create sample webservices helloworld in Java using Jersey and Tomcat
I have tried    to display two spaces in a standard output Java String.

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.