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