0

i am making a program in which i have to allow the user to browse an image and store it with its details in image folder inside the source cod my code is working working in netbeans but when i make jar file the code is cot able to store image. i have stored some images through netbeans and able to access them using image/imanename.jpg but can't able to store images. help me as soon as possible. thank you the code i tried is

            File f = new File(s);
            long size=f.length();
            FileInputStream fis1=new FileInputStream(f);
            FileOutputStream fos2=new FileOutputStream("image/"+tfpn.getText()+".jpg");
            byte b[]=new byte[10000];
            int r=0;
            long count=0;
            while(true)
            {
                r=fis1.read(b,0,10000);
                fos2.write(b,0,10000);
                count = count+r;
                if(count==size)
                break;
                System.out.println(count);
            }
            System.out.println("File copy complete");
344
  • 47
  • 1
  • 3

1 Answers1

0

When you generate a jar file, it is in a compressed format (like .zip). You cannot simply save a file into the jar. It is technically possible, but is complicated. See here for more info.

The reason it works in netbeans is because when you run a file in netbeans, the code is not yet compressed into a jar. Once you make the jar file, you cannot simply write files into the jar. Try and save your file in a normal directory (not within the jar).

Community
  • 1
  • 1
Moose
  • 118
  • 5
  • but if i save it in normal directory i cant access it when i want to display in a label as for that the image should be local otherwise imageicon class will be unable to load it – 344 May 15 '14 at 09:03
  • You can access a file previously loaded into the jar file usung a resource locator. See this site for a good description and tutorial http://math.hws.edu/javanotes/c13/s1.html scroll down to the subcategory on resources. – Moose May 17 '14 at 14:35