简体   繁体   中英

Executable Jar file can't read internal JSON file

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)

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();
                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM