Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Run Java app with admin rights

Subject: Run Java app with admin rights

From: Felix

Date: 11 Oct, 2011 14:54:26

Message: 1 of 4

Hi,

I created an java application that updates my compiled MATLAB application (including the new MCR). Since I install my application in the ProgramFiles folder of Windows it will require admin rights to write new files.

Is there a way to start the java application in MATLAB and tell Windows to either check if the user has the rights (and authorizes the admin use) or otherwise ask for the admin password?

Something like:
path_to_java = findJavaPath;
dos('path_to_java\javaw.exe /adminrights /run path_to_app\myapp.jar');

Thanks
Felix

Subject: Run Java app with admin rights

From: Felix

Date: 27 Oct, 2011 08:47:29

Message: 2 of 4

So I found this nice description on how to embed a manifest and elevate the rights of the user to admin:

http://mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html

I then used Launch4j (http://launch4j.sourceforge.net/) to wrap my jar and added the manifest. It works fine when I double-click on the created exe-file (MyJavaApp.exe), the Java App then has admin rights. Unfortunately if I call the exe-file from MATLAB with dos('MyJavaApp.exe') it won't work. The return status is -1 and the returned message is empty.

Does anyone know how to adjust the sniplet code using ShellExecuteE, given in this example, to work with MATLAB:

SHELLEXECUTEINFO sinfo;
memset(&sinfo, 0, sizeof(SHELLEXECUTEINFO));
sinfo.cbSize = sizeof(SHELLEXECUTEINFO);
sinfo.fMask = SEE_MASK_FLAG_DDEWAIT |
SEE_MASK_NOCLOSEPROCESS;
sinfo.hwnd = NULL;
sinfo.lpFile = argv[0];
sinfo.lpParameters = spawnCmdLine;
sinfo.lpVerb = L"runas"; // <<-- this is what makes a UAC prompt show up
sinfo.nShow = SW_SHOWMAXIMIZED;

// The only way to get a UAC prompt to show up
// is by calling ShellExecuteEx() with the correct
// SHELLEXECUTEINFO struct. Non privlidged applications
// cannot open/start a UAC prompt by simply spawning
// a process that has the correct XML manifest.
BOOL result = ShellExecuteEx(&sinfo);

Subject: Run Java app with admin rights

From: Yair Altman

Date: 22 Mar, 2012 08:07:29

Message: 3 of 4

"Felix " <m@r.net> wrote in message <j8b5r1$83$1@newscl01ah.mathworks.com>...
> So I found this nice description on how to embed a manifest and elevate the rights of the user to admin:
>
> http://mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html
>
> I then used Launch4j (http://launch4j.sourceforge.net/) to wrap my jar and added the manifest. It works fine when I double-click on the created exe-file (MyJavaApp.exe), the Java App then has admin rights. Unfortunately if I call the exe-file from MATLAB with dos('MyJavaApp.exe') it won't work. The return status is -1 and the returned message is empty.
>
> Does anyone know how to adjust the sniplet code using ShellExecuteE, given in this example, to work with MATLAB:
>
> SHELLEXECUTEINFO sinfo;
> memset(&sinfo, 0, sizeof(SHELLEXECUTEINFO));
> sinfo.cbSize = sizeof(SHELLEXECUTEINFO);
> sinfo.fMask = SEE_MASK_FLAG_DDEWAIT |
> SEE_MASK_NOCLOSEPROCESS;
> sinfo.hwnd = NULL;
> sinfo.lpFile = argv[0];
> sinfo.lpParameters = spawnCmdLine;
> sinfo.lpVerb = L"runas"; // <<-- this is what makes a UAC prompt show up
> sinfo.nShow = SW_SHOWMAXIMIZED;
>
> // The only way to get a UAC prompt to show up
> // is by calling ShellExecuteEx() with the correct
> // SHELLEXECUTEINFO struct. Non privlidged applications
> // cannot open/start a UAC prompt by simply spawning
> // a process that has the correct XML manifest.
> BOOL result = ShellExecuteEx(&sinfo);


Matlab has an internal JVM (JRE actually). Have you tried running your Java code directly from Matlab rather than viw Windows executable?

Something like this:

    import path1/path2/myApp.jar
    com.mydomain.myApp.main();

Yair Altman
http://UndocumentedMatlab.com
Read my Matlab-Java Programming book
 
 

Subject: Run Java app with admin rights

From: Yair Altman

Date: 22 Mar, 2012 08:27:23

Message: 4 of 4

> import path1/path2/myApp.jar
> com.mydomain.myApp.main();


Correction:
   javaaddpath path1/path2/myApp.jar
   com.mydomain.myApp.main();

Yair Altman
http://UndocumentedMatlab.com
Read my Matlab-Java Programming book
 
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us