Running Windows commandline from Java as an Admini

2019-02-19 17:56发布

I am running Windows 7 Pro and working on a Java application in Eclipse. I need Eclipse to send user-specified commands (such as 'chkdsk C:') to the command prompt and then output to the console in Eclipse whatever the command prompt would have printed. I have the sending commands working and the receiving text back. However, when I to run chkdsk I need to have admin privileges for the command session. I see from the thread here:

Java: run as administrator

that one way to do this is through a .manifest file. However, I am having trouble understanding how to create a manifest file for Java:

Does the .manifest file just go into the Eclipse workspace with the .CLASS files? If I put it there will it run automatically to start my program in admin mode whenever I run the program?

The link given from the above thread:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

seems to be Visual-Studio specific, will there example code work for a Java program .manifest file? Do I need to create the .manifest file in Visual Studio or is it just a text file?

Also, the name of the manifest file is yourProgram.exe.manifest ... Java as I understand it doesn't create executables of the .exe variety does it? Should the manifest file be named as above or does it need a name like yourProgram.CLASS.manifest?

Thank you for any help!

1条回答
看我几分像从前
2楼-- · 2019-02-19 18:55

The application manifest described here, and the Java manifest file (in the JAR file at META-INF/MANIFEST.MF), are two completely separate concepts that share only a name. There's nothing in META-INF/MANIFEST.MF that will help a Java executable gain elevation.

Besides, its the JVM that needs the elevation, not the class files. Putting the .manifest file with all your class files will not achieve anything.

If I had to do something like this, my preferred approach would be to use a program such as elevate.exe to call CHKDSK. elevate.exe can be found at this blog article linked to in the question you've mentioned. I haven't tested elevate.exe, and the author originally wrote it for Windows Vista, so I don't know how well it works on Windows 7.

It seems that CHKDSK is the only part of your app that requires elevation. If so, it would make sense from a security point-of-view not to have the whole app elevated all of the time when most of the time it can manage without.

查看更多
登录 后发表回答