3

I made a small java app that copies a directory from a CD to the HD. I made the program using Windows Vista and it worked, but when i ran it in Windows 7, it fails.

The main problem is that a folder inside the Program Files folder needs to be created.

I used DestinationFolder.mkdirs(), but it fails creating it

This is the java code:

public void Install_App()
{
    File srcFolder = new File(System.getProperty("user.dir") + "\\WINDOWS");
    File destFolder = new File("C:\\Program Files\\test1\\test2\\");
    if (srcFolder.exists())
    {
        try{
            if(!destFolder.exists())
        {
            destFolder.mkdirs();
        }
            copyFolder(srcFolder,destFolder,1);
        }catch(IOException e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString());
            error=true;
            System.exit(0);
            }
    } else 
    {
        JOptionPane.showMessageDialog(null, "Error. Source Directory doesn't exist.");
        error=true;
    };
} 

... and then there is a copyfolder function that copies the files with inputstream and outputstream.

The problem is that the folder is never created. My login user is an administrator. And as i said, it worked in Vista.

Could you help me, please?

Thanks.

The thing is that i created this app in java to run it in Windows and Mac. In Windows it should autorun with and autorun.inf like this:

[autorun]
OPEN=java_app.bat

then this bat will run this:

@echo off
start javaw -jar "java_app.jar"
EXIT

so how can i modify it to run it as administrator automatically? The main idea of this java app is simplify the process of install & use an external application no matter which OS are you using. If I have to ask the user to run it as admin it will loose it's sense (of been simple of use).

user897013
  • 165
  • 1
  • 3
  • 9
  • 2
    UAC says no!, see http://stackoverflow.com/questions/2709531/windows-7-create-folder-in-program-files-failing-in-c-sharp-code-even-thought – Alex K. Nov 24 '11 at 13:27

3 Answers3

1

I am guessing you are running your code as regular user.

Writing into Program Files directory as a regular-user is by default blocked by UAC under Windows 7. That's why your Java code fails to create directories.

Try running your Java code from a privileged shell. You can have one by Start > [type cmd] > [right-click on 'cmd.exe' and select "Run as administrator"]. Now, run your compiled code with java -jar or java -classpath from the administrator command prompt. It should work now.

Automating the UAC prompt:

You need to create a manifest file as described in detail at [1] and [2] to let Windows/UAC know that your program would need elevated privileges.

Also check this [3] utility called elevate that would spawn your program as child process while handling the UAC permission requests all being made from the parent (elevate) program itself.

[1] [http://msdn.microsoft.com/en-us/library/aa511445.aspx][2] 
[2] [http://msdn.microsoft.com/en-us/library/bb756929.aspx][3]
[3] [http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/03/27/elevate-a-process-at-the-command-line-in-vista.aspx][4]
gsbabil
  • 7,505
  • 3
  • 26
  • 28
  • The thing is that i created this app in java to run it in Windows and Mac. In Windows it should autorun with and autorun.inf like this: – user897013 Nov 24 '11 at 19:54
  • Thanks for your answer. I added some comments on my original question just to try to explain why i need this. – user897013 Nov 24 '11 at 20:16
  • @user897013: You need to create a manifest file as described in detail at [1] and [2] to let Windows/UAC know that your program would need elevated privileges. [1] http://msdn.microsoft.com/en-us/library/aa511445.aspx [2] http://msdn.microsoft.com/en-us/library/bb756929.aspx – gsbabil Nov 25 '11 at 00:48
  • @user897013: Also check this [1] utility called `elevate` that would spawn your program as child process while handling the `UAC` permission requests all being made from the parent (`elevate`) program itself. [1] http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/03/27/elevate-a-process-at-the-command-line-in-vista.aspx – gsbabil Nov 25 '11 at 00:56
  • Thanks for your help, but are you sure that you can use this code in Java? it looks like more a code for VB – user897013 Nov 25 '11 at 10:14
0

This is all the permission problems. I have the same problem on my machine. Nothing is wrong with your java code. I tried to create folder using command line and got "Access Denied".

C:\Users\alexr>mkdir "C:\Program Files\mytest"
Access is denied.

So, the solution is whether to create folder in other location or run as administrator. As @Alex K. aready said, refer to this post to learn how to get such permissions.

Windows 7 Create Folder in "Program Files" failing in C# code even thought I have admin rights!

Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

You do not have the proper privileges to create directories in Program Files. You must start the application with administrative privileges.

An important thing to learn is that when you are developing your applications you should never write them to save/modify data inside Program Files; instead they should either write to AppData our My Documents.

Modifying files in Program Files has been severely deprecated ever since Windows Vista, and even earlier than that. You should try and follow this rule from the start, or it means headaches to rewrite your entire application if you ever want to publish it online.

Stmated
  • 477
  • 5
  • 17
  • @Stamted thanks for your answer, but the application has to be installed in a known folder inside Program Files folder. – user897013 Nov 24 '11 at 20:19