Solved Reading file inside plugin jar

Discussion in 'Plugin Development' started by GriffinPvP, Mar 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    GriffinPvP

    I'm searching for a way to read a file that's inside my plugin's jar, I found this method, but it just doesn't work:
    Code:java
    1. InputStream in = getClass().getResourceAsStream("items.csv");

    I get a NPE:
    Code:
    [15:25:08] [Server thread/WARN]: java.lang.NullPointerException
    [15:25:08] [Server thread/WARN]:    at java.io.Reader.<init>(Unknown Source)
    [15:25:08] [Server thread/WARN]:    at java.io.InputStreamReader.<init>(Unknown Source)
    [15:25:08] [Server thread/WARN]:    at com.griffinpvp.kitpvp.util.ItemUtil.getItemStackFromString(ItemUtil.java:22)
    [15:25:08] [Server thread/WARN]:    at com.griffinpvp.kitpvp.command.staff.ItemExec.onCommand(ItemExec.java:28)
    [15:25:08] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [15:25:08] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196)
    [15:25:08] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457)
    [15:25:08] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)
    What am I doing wrong?
     
  2. Offline

    StealerSlain

    Some more code?
     
  3. Offline

    GriffinPvP

    Then I'm just doing what I was doing with an external file, and that worked without any errors... I just need to get a working BufferedReader from a file inside the jar instead of one from an external file.
     
  4. Offline

    StealerSlain

    Hm... Do you think it's good idea to store the file in plugin.jar? Okay, I found some code about readers, maybe it will help you.
    Code:java
    1. BufferedReader reader = null;
    2.  
    3. try {
    4. File file = new File("sample-file.dat");
    5. reader = new BufferedReader(new FileReader(file));
    6.  
    7. String line;
    8. while ((line = reader.readLine()) != null) {
    9. System.out.println(line);
    10. }
    11.  
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. } finally {
    15. try {
    16. reader.close();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
     
  5. Offline

    GriffinPvP

    Thanks, that's what I'm doing to read the file, the thing is, I wanted to store them inside the jar, but it's really not that important. How would I create an external file from a file already existent inside the jar? I know I could just set the default stuff, but it's 7500 lines of items and aliases, how would I just copy all the lines to an external file?
     
  6. Offline

    RawCode

    files inside jar and files not inside jar require different methods to read and write...
     
  7. Offline

    GriffinPvP

    RawCode Yes, I know, I'm trying to find out how to read files inside jars... I've looked on StackOverflow, but I couldn't find out, so I came here...
     
  8. Offline

    Barinade

    try adding getClassLoader() after getClass()
     
    GriffinPvP likes this.
  9. Offline

    RawCode

    stackoverflow have solution, try a bit different keywords

    protip - jar is zip archive...
     
  10. Offline

    GriffinPvP

Thread Status:
Not open for further replies.

Share This Page