0

I'm trying to include .bin files in my .jar file

this is a function which reads from a file

public Object read(String filepath) throws URISyntaxException{
    Object result=null;
    try{
    ObjectInputStream Reader=new ObjectInputStream(new FileInputStream(new File(this.getClass().getResource("/gui/Files/"+filepath).toURI())));
    result = Reader.readObject();
    }
    catch(IOException e){
    System.out.println(e);
    }
     catch (ClassNotFoundException ex) {
        Logger.getLogger(Fmanager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

it works and reads files from this path "\build\classes\gui\Files" Does the file cause problem?? could you tell me what is the problem please!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Possible duplicate of [https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – cdbbnny Dec 16 '17 at 01:32
  • Some of this copy/paste comment is irrelevant, but the rest is spot on, so offering it as-is.. Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] **must be accessed by URL** rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Dec 17 '17 at 03:38
  • As a result of that, in combination with it being a `.bin` file, it will be necessary to access it by URL, copy the bytes out to a `File` on the local file system, **then** load the binary from the file. – Andrew Thompson Dec 17 '17 at 03:40

0 Answers0