Run java application as administrator priviledge in XP - java

I have created a small program in java which reads the UninstallString of an application from registry and then using
Process p = Runtime.getRuntime.exec("command")
to uninstall this application. But this task require administrator priviledge.
On Windows Vista, 7 and 8 this work fine by creating a bat file with the following command "java -jar java_app.jar", right click on the bat file and run as administrator.
But on XP, my account is an administrator account but still it fails to run the java program as administrator.
Can anyone provide me with some solutions which can solve this problem?

You can use RunAs command:
RUNAS /TrustLevel:Unrestricted notepad.exe
You can find trust levels available on you system:
RUNAS /ShowTrustLevels
The following liks explains that in more details http://blog.johnmuellerbooks.com/2011/04/26/simulating-users-with-the-runas-command.aspx

Related

Run batch file as administrator in java [duplicate]

I am developing a small shutdown scheduler project in which i have to put the computer in "Stand By" mode. The command that i am using is
Runtime.getRuntime().exec("cmd /c Powrprof.dll,SetSuspendState ");
This command requires Admin rights which i don't know how to get. Also while searching for previous answers i found i can use elevate.exe as
Runtime.getRuntime().exec("c:/elevate Rundll32.exe Powrprof.dll,SetSuspendState ");
Elevate.exe is doing the task but is consuming too much of time i.e. making the software slow. Is there any other speedy way? I am using Netbeans IDE.
Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\"");
Also plz see comments
Running as admin without Admin rights
You have a few options
A. Create a shortcut with admin priv.
The shortcut will run cmd /c Rundll32.exe Powrprof.dll,SetSuspendState
Your Java code will run the shortcut:
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start \"\" \"myshortcut.lnk\"")
Right click the shortcut icon > properties > advanced > run as administrator
B. Run the java process as administrator
Again, create a shortcut and set to run as administrator. Any processes spawned will also have admin privileges. Your java code will run:
rt.exec("cmd /c Powrprof.dll,SetSuspendState")
C. Use JNA to directly call SetSuspendState routine. The Java process will require admin priv (like B), but you won't have to spawn a process. If you like this, I can provide source code.
D. Use wizmo utility: wizmo quiet standby
Add parameter /savecred
runas /profile /user:Administrator /savecred
Input the password one times. In future OS will not ask you password.
I'm using Windows 10. IDK why but runas isn't working and isn't reporting any errors.
I found this answer on superuser.com:
powershell -Command "Start-Process 'cmd.exe /c Powrprof.dll,SetSuspendState ' -Verb runAs"
No password required if you have permission to elevate.
No shortcut required on client machine
No dependency on runas
Requires powershell
Powershell is installed by default on Windows since Windows 8 and Windows Server 2008 R2, according to an answer found on serverfault.com.

running application from cmd as administrator through java code

I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code.r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:
How to run any application from CMD in windows 8 with administrator privilliges?
The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.
Thanks
You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.
I had a similar issue to you in the past, and I solved it using PsExec, running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.
Using your example you could run:
PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe
The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.
This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.

Installation failed due to low priviledge on Win8

I am using Java code to install an EXE program on Win8, by executing this command "java -jar installapp.jar" in CML. The command line window is NOT opened by "Run as administrator", but the current user is a member of administrator group.
However, in the same CML window, if I install the EXE directly, it works. It just fails when the installation is executed by Java.
So anyone can give me a tip?
Thanks a lot,
Michael
To correctly install an application which includes writing to system-protected areas can't be done without elevating through UAC. That means the CML window must be Run as administrator.
Java executables are marked with manifest which requests asInvoker privileges. So the process would start with administrator tokens dropped if the parent process wasn't elevated. It's the whole point of UAC: even if you are member of Administrators group, you don't get the full, unlimited privileges until you elevate.
What do you mean by "install the exe directly"?

Batch file to run a java app. How to port it in Mac?

I have a tiny batch file to kick off my java app:
its code is something as simple as
java -jar myapp.jar
(in reality it's slightly more complicated as we set a few properties)
A friend of mine has a mac. He has java installed on his mac.
But how do we run this batch from mac ?
Put the following code into a file and run chmod +x <filename> to make it executable.
#!/bin/sh
java -jar myapp.jar
If the Java app. is a desktop app. with a GUI, a good way to launch it is using Java Web Start. JWS provides the ability to set properties in the JNLP launch file.
JWS works on all desktop JREs. E.G. it will work on Windows, Mac. and *nix.

Java application security permissions

I have a compiled Java Application (.jar file extension, no source code) which will not function correctly without read/write permissions. How do it get it to run with these administrator permissions?
EDIT: This is being run in Windows 7.
This appears to be a question of Windows 7 security, rather than Java security. Java JAR by default have full access that the OS allows.
Can you not create a shortcut and run it as administrator or start a prompt as administrator and run the jar from this prompt. If you have a service, you can set it up to run as administrator.

Categories