FileNotFoundException when exporting .jar

前端 未结 3 889
面向向阳花
面向向阳花 2021-01-21 16:28

In my client/server app I need to send some file (.txt, .doc, etc.) from the client to the server. When I run my code in Eclipse it works, but when I e

相关标签:
3条回答
  • 2021-01-21 17:12

    I found the solution after becoming mad. Windows didn't have privileges to open the files. So run your browser with Administrator privileges and it will work.

    0 讨论(0)
  • 2021-01-21 17:14

    This code is looking a file on the classpath. If there's no a file there it throws FNF. When you work in Eclipse your file is probably in the src, so it's copied to bin. After you archived a file to the jar you can access it either getResource or getResourceAsStream

    InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream(sourceFile.getName())
    

    or using URL. For example

    URL url = new URL("jar:file:/c:/path/to/my.jar!/myfile.txt"); 
    JarURLConnection conn = (JarURLConnection)url.openConnection();
    InputStream inputFile = conn.getInputStream();
    
    0 讨论(0)
  • 2021-01-21 17:25

    You need to copy your resources manually into the jar.

    To do so, use 7zip or winRar or anything else, right click and "open archive". Then drag-and-drop your resouces (e.g. png's etc.) to the appropriate folder (usually root).

    0 讨论(0)
提交回复
热议问题