问题
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).
回答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]
回答2:
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!
回答3:
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.
来源:https://stackoverflow.com/questions/8257765/mkdirs-not-working-in-windows-7