Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access files within folders within jar files?
    primarykey
    data
    text
    <p>I have looked at <a href="https://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file">How to access resources in JAR file?</a> and <a href="https://stackoverflow.com/questions/463972/how-do-i-copy-a-text-file-from-a-jar-into-a-file-outside-of-the-jar">How do I copy a text file from a jar into a file outside of the jar?</a> and many other questiions but couldnt actually get an answer. What I'm trying to do is copy contents of a file in <code>res/CDC.txt</code> 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 <code>FileNotFoundException</code>. So, I figured out why it works on mine. I have a CLASSPATH set to <code>.;D:\myname\Java\JavaFiles</code> 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:</p> <pre><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); } } </code></pre> <p>which I changed to</p> <pre><code>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); } } </code></pre> <p>And this time I always get <code>NullPointerException</code>. So, how to read folders and files that are within jar?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload