Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:20:32+00:00 2026-06-14T02:20:32+00:00

As the title says, I’m wondering if it is possible for a program written

  • 0

As the title says, I’m wondering if it is possible for a program written in Java (and only java) to relaunch himself (preferably a .jar) with administrator privileges, showing in the way the native Windows UAC (in order to make it more trustable for the user), i did my homework and found out that it is possible to accomplish this using bridges between c++ and java, but i would really like to do this as a pure java project.

P.S: In the remote case that this result to be impossible, can someone show me the “easy” way to do this using another language (i mean, I’ve found tutorials, but they are to complicated for something I think it should not be that complicated).

P.S2: In case it is possible to accomplish this, would it work, on other platforms (OS X, Linux)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:20:34+00:00Added an answer on June 14, 2026 at 2:20 am

    It cannot be done in pure java.

    Best bet would be to write this to a file:

    @echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
    @echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
    @echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
    @cscript %temp%\sudo.tmp.vbs
    

    and save it as something.bat in Windows temp directory (as we have access to this).

    You would then execute this from your application using Runtime or ProcessBuilder and exit your application (System.exit(0);).

    You should add an immediate start up check to your application that checks if the program has elevation, if it has proceed if not re-run the batch and exit.

    Here is an example I made (this must be run when compiled as a Jar or it wont work):

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author David
     */
    public class UacTest {
    
        public static String jarName = "UacTest.jar", batName = "elevate.bat";
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            if (checkForUac()) {//uac is on
                JOptionPane.showMessageDialog(null, "I am not elevated");
                //attempt elevation
                new UacTest().elevate();
                System.exit(0);
            } else {//uac is not on
                //if we get here we are elevated
                JOptionPane.showMessageDialog(null, "I am elevated");
            }
    
        }
    
        private static boolean checkForUac() {
            File dummyFile = new File("c:/aaa.txt");
            dummyFile.deleteOnExit();
    
            try {
                //attempt to craete file in c:/
                try (FileWriter fw = new FileWriter(dummyFile, true)) {
                }
            } catch (IOException ex) {//we cannot UAC muts be on
                //ex.printStackTrace();
                return true;
            }
            return false;
        }
    
        private void elevate() {
            //create batch file in temporary directory as we have access to it regardless of UAC on or off
            File file = new File(System.getProperty("java.io.tmpdir") + "/" + batName);
            file.deleteOnExit();
    
            createBatchFile(file);
    
            runBatchFile();
    
        }
    
        private String getJarLocation() {
            return getClass().getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);
        }
    
        private void runBatchFile() {
            //JOptionPane.showMessageDialog(null, getJarLocation());
    
            Runtime runtime = Runtime.getRuntime();
            String[] cmd = new String[]{"cmd.exe", "/C",
                System.getProperty("java.io.tmpdir") + "/" + batName + " java -jar " + getJarLocation()};
            try {
                Process proc = runtime.exec(cmd);
                //proc.waitFor();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        private void createBatchFile(File file) {
            try {
                try (FileWriter fw = new FileWriter(file, true)) {
                    fw.write(
                            "@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
                            + "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
                            + "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
                            + "@cscript %temp%\\sudo.tmp.vbs\r\n"
                            + "del /f %temp%\\sudo.tmp.vbs\r\n");
                }
            } catch (IOException ex) {
                //ex.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As title says, I'm wondering if stacking subviews can slow down an iPhone application.
The title says most of it, I'm wondering if MySQL is suitable, (and if
As title says, mock code to demostrate my problem Driver Class import java.io.*; public
The title says everything. I need to make selecting of the UITableViewCells possible when
Just as the title says: setInterval is only firing its callback once. manifest.json: {
As title says i whant to change my variables in java from my jruby
As title says, how do I call a java class method from a jsp,
As the title says on a website is it possible to tell if a
As title says, is it possible to give a user access to the rich
Title says all. Sample code: ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.