• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to Run a windows program as a specific user from within Java

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 1.5 Windows XP/Server 2003
How can I run Windows command from within a Java application as a particular domain user. I am willing to provide password for this user in the Java program. Java application itself is running as local SYSTEM user.

Runtime class or any other method will do.

Thanks in advance.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds to me like you could use Window's runas command: http://lifehacker.com/software/windows-tip/switch-user-accounts-from-the-command-prompt-290435.php

I found this by googling for "windows run command as different user" - there are a couple more articles that might be helpful to you.
 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply

Runas command requires a user to be present to type in the password. I haven't found another workaround in Windows so far. So I was wondering if I could change the user within Java before it calls the Windows program. I can use either approaches as long as it works unattended.

Any other ideas?
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you calling this Java program? From a scheduled task? From a service? Both allow you to specify the user to run as, which will be stored with its password.
 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java program is running on an application server as a J2EE application. I am trying to invoke a windows utility from within this J2EE application. The windows utility requires elevated permission than the user application server is running as.

So how could I accomplish my goal?

Thanks for your reply
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anne Forumer:

Runas command requires a user to be present to type in the password.



Have you looked at the /savecred option?

Failing that, I'd assume that the password is actually read from standard input. The Process object you get from Runtime.exec allows you to access the standard input as an OutputStream, and therefore to write into that stream. That way, you should be able to provide the password from the Java program.

Hope this helps,

Ilja
 
Rob Spoor
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless the password needs to be filled in in a dialog. This happens quite a lot in Windows.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Unless the password needs to be filled in in a dialog. This happens quite a lot in Windows.



Not in this case.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic