可执行的Jar文件无法读取内部的JSON文件。

huangapple go评论56阅读模式
英文:

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][1] (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 json

<details>
<summary>英文:</summary>

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&#39;ve tried using both `FileReader` and `InputStream` but I can&#39;t get it to work either way.

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

I&#39;m using Google&#39;s JSON simple if that helps too. I also uploaded a file system export of the entire project if that is needed [GDriveLink][1] (It&#39;s a beginner project for school, so it doesn&#39;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(&quot;Links.json&quot;))	 //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 -&gt; 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(&quot;linkSet1&quot;);
//Get 1st link data
link1 = (String) userObject.get(&quot;jlink1&quot;);
link1Cb = (String) userObject.get(&quot;jlink1Cb&quot;);    
System.out.println(link1);
System.out.println(link1Cb);
//Get 2nd link data
link2 = (String) userObject.get(&quot;jlink2&quot;);
link2Cb = (String) userObject.get(&quot;jlink2Cb&quot;);    
System.out.println(link2);
System.out.println(link2Cb);
//Get 3rd link data
link3 = (String) userObject.get(&quot;jlink3&quot;);
link3Cb = (String) userObject.get(&quot;jlink3Cb&quot;);    
System.out.println(link3);
System.out.println(link3Cb);
//Get 4th link data
link4 = (String) userObject.get(&quot;jlink4&quot;);
link4Cb = (String) userObject.get(&quot;jlink4Cb&quot;);    
System.out.println(link4);
System.out.println(link4Cb);
//Get 5th link data
link5 = (String) userObject.get(&quot;jlink5&quot;);
link5Cb = (String) userObject.get(&quot;jlink5Cb&quot;);    
System.out.println(link5);
System.out.println(link5Cb);
//Get first link data
link6 = (String) userObject.get(&quot;jlink6&quot;);
link6Cb = (String) userObject.get(&quot;jlink6Cb&quot;);    
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(&quot;Links.json&quot;);
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 -&gt; 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(&quot;linkSet1&quot;);
//Get 1st link data
link1 = (String) userObject.get(&quot;jlink1&quot;);
link1Cb = (String) userObject.get(&quot;jlink1Cb&quot;);    
System.out.println(link1);
System.out.println(link1Cb);
//Get 2nd link data
link2 = (String) userObject.get(&quot;jlink2&quot;);
link2Cb = (String) userObject.get(&quot;jlink2Cb&quot;);    
System.out.println(link2);
System.out.println(link2Cb);
//Get 3rd link data
link3 = (String) userObject.get(&quot;jlink3&quot;);
link3Cb = (String) userObject.get(&quot;jlink3Cb&quot;);    
System.out.println(link3);
System.out.println(link3Cb);
//Get 4th link data
link4 = (String) userObject.get(&quot;jlink4&quot;);
link4Cb = (String) userObject.get(&quot;jlink4Cb&quot;);    
System.out.println(link4);
System.out.println(link4Cb);
//Get 5th link data
link5 = (String) userObject.get(&quot;jlink5&quot;);
link5Cb = (String) userObject.get(&quot;jlink5Cb&quot;);    
System.out.println(link5);
System.out.println(link5Cb);
//Get first link data
link6 = (String) userObject.get(&quot;jlink6&quot;);
link6Cb = (String) userObject.get(&quot;jlink6Cb&quot;);    
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)


[1]: https://drive.google.com/drive/folders/1V83_4O9pS_thhEAa28Gz34JyGEwvffkQ?usp=sharing
</details>
# 答案1
**得分**: 1
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.
```java
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();
}
}

(Note: I have removed the HTML entity &quot; and kept the Java code intact.)

英文:

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(&quot;/mainFolder/resources/Links.json&quot;), 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 -&gt; parseUserObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}

huangapple
  • 本文由 发表于 2020年8月2日 21:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定