FileNotFoundException when exporting .jar
Asked Answered
P

3

2

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 export the signed JAR of the Applet it doesn't. It throws a FileNotFoundException. I tried saving file in several ways without success.

public static boolean saveFile(File sourceFile) throws IOException {

    DirectoryChooserDialog dialog = new DirectoryChooserDialog();
    filePath = dialog.getDestinationFolder();
    if (filePath != null) {
        InputStream inputFile = ClassLoader.getSystemResourceAsStream(""+sourceFile);

        filePath += File.separator + sourceFile.getName();

        FileOutputStream outputFile = new FileOutputStream(filePath);

        int byteLetti = 0;
        while ((byteLetti = inputFile.read(buffer)) >= 0) {
            outputFile.write(buffer, 0, byteLetti);
            outputFile.flush();
        }

        inputFile.close();

        outputFile.close();

        return true;
    } else
        return false;
}

Alternative code used:

FileInputStream inputFile = new FileInputStream(sourceFile);

Or

InputStream inputFile = ClassLoader.class.getResourceAsStream(""+sourceFile);

Or

InputStream inputFile = FileSaving.class.getResourceAsStream(""+sourceFile);

Original code and every alternative work in Eclipse and stop working when exported.

Polinski answered 19/8, 2012 at 10:6 Comment(1)
If your application creates data dynamically, you should use system specific storage for storing application data. Some kind of java.io.tmpdir or user.home. Not try to find them inside .jar, because it's useless.Tumble
P
1

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();
Picked answered 19/8, 2012 at 10:16 Comment(10)
Hi, I get this error: The method getInputStream() is undefined for the type FilePolinski
I tried using your code but I don't understand how to merge it with my code. Would you do it for me? ThanksPolinski
The same result, OK on eclipse, KO on Jar.Polinski
Made a couple of attempts with 2 type of files: IO error write: playbasket_8.doc IO error write: playbasket_8.pdf (running on eclipse I get those errors on the Console, but the file are correctly created in the file system)Polinski
I have signed mine, in fact it doesn't throws any security exceptions.Polinski
Note that I have hardcoded the outFile because you removed the DirectoryChooserDialog to select the destination folder.Polinski
Yes the DirectoryChooserDialog is wrapping a JFileChooser. I will check some other things.Polinski
The only problem with your last solution is that I can't pack the file within the jar because it's created on runtime by the app itself. (Thanks a lot for your help!)Polinski
Where? On the client side or server side?Picked
If you need "to send some file (.txt, .doc ...) from the client to the server" you can't do it with this code. You need fileupload applet that communicate with servlet.Picked
D
0

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).

Driftage answered 19/8, 2012 at 10:9 Comment(1)
Maybe this would work but my app creates the resources "dinamically".Polinski
P
0

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.

Polinski answered 6/9, 2012 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.