1

I have looked at How to access resources in JAR file? and How do I copy a text file from a jar into a file outside of the jar? and many other questiions but couldnt actually get an answer. What I'm trying to do is copy contents of a file in res/CDC.txt that is in jar, to somewhere out of a jar. Now, on my computer it works but when I try it on different computer I get FileNotFoundException. So, I figured out why it works on mine. I have a CLASSPATH set to .;D:\myname\Java\JavaFiles where all my java files are located in packages. In "JavaFiles" directory there is also "res/CDC.txt". So, when I start my application, it first checks the current directory myapp.jar is located in for "res/CDC.txt", and then it checks "JavaFiles" and finds it. Other computers do not have it. So, this was my initial code:

public final class CT
{

//Other fields
private static CT ct;
private NTSystem nts;
private File f1;
private File f6;
private PrintWriter pw1;
private BufferedReader br1;
//Other fields

public static void main(String[] args)
{
    try
    {
        showMessage("Executing program...");
        ct = new CT();
        ct.init();
        ct.create();
        ct.insertData();
        //Other code
        showMessage("Program executed!");
    }
    catch(Exception e)
    {
        showMessage("An exception occured! Program closed.");
        e.printStackTrace();
        System.exit(0);
    }
}

private void init()
    throws IOException
{
    //Other initialization
    nts = new NTSystem();
    f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
    f6 = new File("res\\CDC.txt");
    br1 = new BufferedReader(new FileReader(f6));
    //Other initialization
    showMessage("Initialized");
}

private void create()
    throws IOException
{
    //Makes sure file/dir exists, etc

    pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
    //Other Stuff

    showMessage("Created");
}

private void insertData()
    throws IOException
{
    String line = br1.readLine();
    while(line != null)
    {
        pw1.println(line);
        line = br1.readLine();
    }

    //Other stuff
    showMessage("Data inserted");
}

private static void showMessage(String msg)
{
    System.out.println(msg);
}

}

which I changed to

public final class CT
{

//Other fields
private static CT ct;
private NTSystem nts;
private byte[] buffer;
private File f1;
private URL url1;
private FileOutputStream fos1;
private InputStream is1;
//Other fields

public static void main(String[] args)
{
    try
    {
        showMessage("Executing program...");
        ct = new CT();
        ct.init();
        ct.create();
        ct.insertData();
        //Other code
        showMessage("Program executed!");
    }
    catch(Exception e)
    {
        showMessage("An exception occured! Program closed.");
        e.printStackTrace();
        System.exit(0);
    }
}

private void init()
    throws IOException
{
    //Other initialization
    nts = new NTSystem();
    buffer = new byte[4096];
    f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
    url1 = getClass().getClassLoader.getResource("res\\CDC.txt"); //Also tried url1 = ct.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = this.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = CT.getClass().getClassLoader.getResource("res\\CDC.txt");
    is1 = url1.openStream();
    //Other initialization
    showMessage("Initialized");
}

private void create()
    throws IOException
{
    //Makes sure file/dir exists, etc

    pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
    //Other Stuff

    showMessage("Created");
}

private void insertData()
    throws IOException
{
    int read = is1.read(buffer);
    while(line != null)
    {
        fos1.write(buffer, 0, read);
        read = is1.read(buffer);
    }

    //Other stuff
    showMessage("Data inserted");
}

private static void showMessage(String msg)
{
    System.out.println(msg);
}

}

And this time I always get NullPointerException. So, how to read folders and files that are within jar?

Thanks

Community
  • 1
  • 1
Franko Leon Tokalić
  • 1,457
  • 3
  • 22
  • 28

1 Answers1

0

You will want to use getSystemResourceAsStream() to read the contents from files in a jar.

This of course is assuming the file is actually on the classpath of the other users computers.

Jyro117
  • 4,519
  • 24
  • 29