2

I have an application with a read-write script that will read and write a set of links and checkboxes and either read or write a JSON file. Every part of the application works fine except when I try to read a JSON file. I've tried using both FileReader and InputStream but I can't get it to work either way.

FileWriter works fine in the Eclipse IDE, (Latest version, running JDK 11, same as my PC's version of java) but not when I pack it into a runnable jar. I also tried InputStream but that doesn't work either in the IDE or packaged jar file.

I'm using Google's JSON simple if that helps too. I also uploaded a file system export of the entire project if that is needed GDriveLink (It's a beginner project for school, so it doesn't look too pretty inside.)

Code for FileWriter:

public void ReadToJson() { //I've tried this as static and non static

                //JSON parser object to parse read file
                JSONParser jsonParser = new JSONParser();
                 
                try (FileReader reader = new FileReader("Links.json"))   //line 93  

                {
                    
                    //Read JSON file
                    Object obj = jsonParser.parse(reader);
         
                    JSONArray userList = (JSONArray) obj;
                    System.out.println(userList);
                     
                    //Iterate over user array
                    userList.forEach( emp -> parseUserObject( (JSONObject) emp ) );
         
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            
            private static void parseUserObject(JSONObject user) 
            {
                //Get user object within list
                JSONObject userObject = (JSONObject) user.get("linkSet1");
                
                //Get 1st link data
                link1 = (String) userObject.get("jlink1");
                link1Cb = (String) userObject.get("jlink1Cb");    
                System.out.println(link1);
                System.out.println(link1Cb);
                
              //Get 2nd link data
                link2 = (String) userObject.get("jlink2");
                link2Cb = (String) userObject.get("jlink2Cb");    
                System.out.println(link2);
                System.out.println(link2Cb);
                
              //Get 3rd link data
                link3 = (String) userObject.get("jlink3");
                link3Cb = (String) userObject.get("jlink3Cb");    
                System.out.println(link3);
                System.out.println(link3Cb);
                
              //Get 4th link data
                link4 = (String) userObject.get("jlink4");
                link4Cb = (String) userObject.get("jlink4Cb");    
                System.out.println(link4);
                System.out.println(link4Cb);
                
              //Get 5th link data
                link5 = (String) userObject.get("jlink5");
                link5Cb = (String) userObject.get("jlink5Cb");    
                System.out.println(link5);
                System.out.println(link5Cb);
                
              //Get first link data
                link6 = (String) userObject.get("jlink6");
                link6Cb = (String) userObject.get("jlink6Cb");    
                System.out.println(link6);
                System.out.println(link6Cb);
                
            }
            
    }
                

Console logs for FileWriter after calling the ReadToJson() function:

java.io.FileNotFoundException: Links.json (The system cannot find the file specified)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
        at java.base/java.io.FileReader.<init>(FileReader.java:60)
        at mainFolder.common.RWJsonBTabs.ReadToJson(RWJsonBTabs.java:93)
        at mainFolder.views.panelBrowser$3.actionPerformed(panelBrowser.java:159)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6631)
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
        at java.desktop/java.awt.Component.processEvent(Component.java:6396)
        at java.desktop/java.awt.Container.processEvent(Container.java:2263)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5007)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Code for InputStream:

public void ReadToJson() {
//JSON parser object to parse read file
                JSONParser jsonParser = new JSONParser();
                 
                try (InputStream inputStream = getClass().getResourceAsStream("Links.json");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) //Line 94       
                {
                    
                    //Read JSON file
                    Object obj = jsonParser.parse(reader);
         
                    JSONArray userList = (JSONArray) obj;
                    System.out.println(userList);
                     
                    //Iterate over user array
                    userList.forEach( emp -> parseUserObject( (JSONObject) emp ) );
         
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            
            private static void parseUserObject(JSONObject user) 
            {
                //Get user object within list
                JSONObject userObject = (JSONObject) user.get("linkSet1");
                
                //Get 1st link data
                link1 = (String) userObject.get("jlink1");
                link1Cb = (String) userObject.get("jlink1Cb");    
                System.out.println(link1);
                System.out.println(link1Cb);
                
              //Get 2nd link data
                link2 = (String) userObject.get("jlink2");
                link2Cb = (String) userObject.get("jlink2Cb");    
                System.out.println(link2);
                System.out.println(link2Cb);
                
              //Get 3rd link data
                link3 = (String) userObject.get("jlink3");
                link3Cb = (String) userObject.get("jlink3Cb");    
                System.out.println(link3);
                System.out.println(link3Cb);
                
              //Get 4th link data
                link4 = (String) userObject.get("jlink4");
                link4Cb = (String) userObject.get("jlink4Cb");    
                System.out.println(link4);
                System.out.println(link4Cb);
                
              //Get 5th link data
                link5 = (String) userObject.get("jlink5");
                link5Cb = (String) userObject.get("jlink5Cb");    
                System.out.println(link5);
                System.out.println(link5Cb);
                
              //Get first link data
                link6 = (String) userObject.get("jlink6");
                link6Cb = (String) userObject.get("jlink6Cb");    
                System.out.println(link6);
                System.out.println(link6Cb);
            }

Console logs for InputStream after calling the ReadToJson() function:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:167)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at mainFolder.common.RWJsonBTabs.ReadToJson(RWJsonBTabs.java:94)
    at mainFolder.views.panelBrowser$3.actionPerformed(panelBrowser.java:159)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6631)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6396)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5007)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Tyler Moen
  • 81
  • 5
  • 1
    A .jar file is a zip archive. An entry in an archive *is not a file,* it’s just a subsequence of compressed bytes in the archive file. You cannot use FileReader or FileInputStream or File to read it. Use `ReadToJson.class.getResourceAsStream("/Links.json")` instead. See https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String). – VGR Aug 02 '20 at 13:46
  • @VGR I just tried that but then the line below, `Object obj = jsonParser.parse(reader);` says "_The method parse(String) in the type JSONParser is not applicable for the arguments (InputStream)_" Then it tells me to convert it to a string which gives more errors. EDIT: I'm I supposed to replace the parser too? CODE: `try (InputStream reader = RWJsonBTabs.class.getResourceAsStream("/Links.json")) { //Read JSON file Object obj = jsonParser.parse(reader); JSONArray userList = (JSONArray) obj;` – Tyler Moen Aug 02 '20 at 13:54
  • You can convert an InputStream to a Reader using the [InputStreamReader](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/InputStreamReader.html) class: `new InputStreamReader(ReadToJson.class.getResourceAsStream("/Links.json"), StandardCharsets.UTF_8)` (Do not omit the charset, or you will get different behavior on Windows systems versus non-Windows systems.) – VGR Aug 02 '20 at 13:58
  • @VGR I'm not sure if I'm doing is right but it changed it to this `try (InputStreamReader reader = new InputStreamReader(RWJsonBTabs.class.getResourceAsStream("/Links.json")))` If I just put InputStream it says it can't convert. either way still gives the same errors. I'm on win10 and the charset didn't change anything – Tyler Moen Aug 02 '20 at 14:13
  • The NullPointerException is occurring because getResourceAsStream is returning null, which indicates that it cannot find the resource. Are you certain Links.json is present in your .jar file? Under what directory is the .jar entry? – VGR Aug 02 '20 at 14:58
  • @VGR Links.json is in the root directory above src, its in the file system. I don't know how to check the exported jar if that's what you're asking. – Tyler Moen Aug 02 '20 at 15:12
  • A .jar file is actually a zip file with a different extension. So, you can either use your IDE’s file explorer to examine the .jar, or you can make a copy of the .jar and rename the copy so it has a .zip extension, then open it with any zip tool (including Windows’ file explorer). – VGR Aug 02 '20 at 15:49
  • THANK YOU! I opened the jar and I couldn't find the JSON file, but when I placed it in a different folder other than root it was able to read its path, thank you. – Tyler Moen Aug 03 '20 at 02:05

1 Answers1

1

CREDIT GOES TO @VGR FOR HELPING ME OUT IN COMMENTS

I ended up moving the JSON file from root directory into a folder which was what did the trick. I also changed the code around thanks to @VGR.

public void ReadToJson() {
                
                //JSON parser object to parse read file
                JSONParser jsonParser = new JSONParser();
                 // /Tigris Auxilium/src/mainFolder/resources/Links.json
                try (InputStreamReader reader = new InputStreamReader(RWJsonBTabs.class.getResourceAsStream("/mainFolder/resources/Links.json"), StandardCharsets.UTF_8))   
                {
                    
                    //Read JSON file
                    Object obj = jsonParser.parse(reader);
         
                    JSONArray userList = (JSONArray) obj;
                    System.out.println(userList);
                     
                    //Iterate over user array
                    userList.forEach( emp -> parseUserObject( (JSONObject) emp ) );
         
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
Tyler Moen
  • 81
  • 5