Results 1 to 6 of 6
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Unable To Create Folders

    Why am I unable to create folders?
    I am sure I did everything correct.

    Here's my code:
    Java Code:
    import java.io.File;
    
    
    public class Main {
    	public static void main(String[] args) {
    		File file = new File("c:/program files/folder check 12 12");
    		System.out.println(file.mkdir());
    	}
    
    }
    Last edited by PhQ; 09-01-2012 at 10:43 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Unable To Create Folders

    This code worked for me:
    Java Code:
          File file = new File("c:/Temp/folder check 12 12");
          System.out.println(file.mkdir());
    I had a Temp folder
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Unable To Create Folders

    Quote Originally Posted by Norm View Post
    This code worked for me:
    Java Code:
          File file = new File("c:/Temp/folder check 12 12");
          System.out.println(file.mkdir());
    I had a Temp folder
    I am trying to make an installer program, but it doesn't let me to create folders in Program Files folder.
    I think it's because I don't have the permissions to do it.
    If I do it myself, a window comes up saying Folder Access Denied and I need to click continue.

    Is it possible to get those permissions for my JAVA application?

    If not - is there a workaround for this?
    I have tried using Runtime.exec to do this the 'cmd mkdir' way but I had no luck.
    Last edited by PhQ; 09-02-2012 at 01:39 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Unable To Create Folders

    Check the doc for the OS you are working on.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Unable To Create Folders

    Java Code:
    if(canCreate(file)){
    				startInstall(file.getAbsolutePath());
    			}else{
    				if(!file.mkdirs()){
    					JOptionPane.showMessageDialog(this, "Sorry, you don't have the correct permissions to write in this folder. Please choose a diffrent folder.", "Permission Error", JOptionPane.ERROR_MESSAGE);
    				}else{
    					startInstall(file.getPath());
    				}
    			}
    and
    Java Code:
    public boolean canCreate(File file){
    		return file.canWrite();
    	}
    totally fixed the problem for me.
    I think the permissions has something to do with this: Permissions in the Java(TM) 2 SDK if anyone else is wondering.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Unable To Create Folders

    Is it possible to check whether or not you can create the folder before creating it?
    I have found that if you use Files.createDirectory() you get an AccessDeniedException. Is it possible to find out if you will get the exception before creating the folder?

Similar Threads

  1. Replies: 0
    Last Post: 11-22-2011, 01:06 PM
  2. Create folders dynamically in diff system (known path + IP)
    By wiki.horizon in forum Advanced Java
    Replies: 5
    Last Post: 06-28-2011, 09:29 AM
  3. Unable to create and write files
    By DrKilljoy in forum New To Java
    Replies: 4
    Last Post: 09-05-2010, 12:55 AM
  4. Replies: 4
    Last Post: 10-10-2009, 12:26 AM
  5. Replies: 20
    Last Post: 12-17-2007, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •