-1

I write java in netbeans.I want to move files and create files.When I run the program on netbeans it works. Files creates and moved. But when I run program on the jar it doesnot copy or move. I give permisson jar and files for read , write. But it does not works.And I open jar with root user. how can i fix this?

this is my code to copy

package aaa;

import java.io.File;  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MoveFolder 
{
    public   int copy(String from,String where  )
    {   
        int onay=0;
        File srcFolder = new File(from);
        File destFolder = new File(where);

        //make sure source exists
        if(!srcFolder.exists()){

           System.out.println("Directory does not exist."); 
            onay = -1;
          // JOptionPane.showMessageDialog(null, "Directory does not exist.");

        }else{

           try{
            copyFolder(srcFolder,destFolder);
           }catch(IOException e){
            e.printStackTrace(); 
                onay = -1;
           //    JOptionPane.showMessageDialog(null, "Was not Copied.");
           }
        }

        System.out.println("Done");
         return onay;
    }

    public   void copyFolder(File src, File dest)
        throws IOException{


        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
          //          JOptionPane.showMessageDialog(null, "Directory copied from " + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types

            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 



                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                   out.write(buffer, 0, length);
                }

                in.close();
                out.close();
                System.out.println("File copied from " + src + " to " + dest);
         //       JOptionPane.showMessageDialog(null, "File copied from " + src + " to " + dest);
        }
    }

}
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • Please post the code, command you use to start your program and prints/exception you are getting. – gammay Sep 10 '12 at 07:46
  • i edited my question. it doesnt matter. I think the problem is about permissions – CompEng Sep 10 '12 at 07:48
  • Add exception you are getting. – yatul Sep 10 '12 at 07:50
  • are you copying to a directory that doesn't exist? – gigadot Sep 10 '12 at 07:51
  • then you need to tell us what exception did you get? – gigadot Sep 10 '12 at 08:06
  • What exception ? it s work when I run it on netbeans but it not copy on jar? – CompEng Sep 10 '12 at 08:23
  • the exception that is thrown when you run from jar file. if it doesn't work then it should be thown unless you consume the exception without doing anything, which is bad. – gigadot Sep 10 '12 at 08:32
  • Providing [SSCCE](http://sscce.org/) could help to solve your problem! – cubanacan Sep 10 '12 at 08:39
  • Provide the *exception.* Not a paraphrase, or a translation, or what you think it says. The fact that you don't understand it makes you the last person who should be doing any of those things. – user207421 Sep 10 '12 at 12:06
  • I edit my question and I use this class. It works on run it on netbeans butit doesnt work on jar. pls help – CompEng Sep 10 '12 at 14:43
  • 1
    Your code does not contain a `main()` method. Please edit your question with code that recreates your exact problem so that we can help you. In particular, we need to know how you tell this class which files to copy. Also, where are those files located in your file system? – Code-Apprentice Sep 10 '12 at 14:52
  • System.getProperty("user.dir") does not get the true path. /home/user/(missing desktop)/.. why desktop is missing – CompEng Sep 10 '12 at 15:25
  • Until you provide the actual exception this is not a real question. – user207421 Sep 14 '12 at 23:47

1 Answers1

1

Why are you copying files with IO streams? Why not just do

File f; // bla bla init
File otherFile;
f.renameTo(otherFile);

Also, you need read and write permissions on the files you're trying to move (to), not on the jars you're executing ...

Nim
  • 631
  • 6
  • 12