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 9144129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:11:38+00:00 2026-06-17T10:11:38+00:00

I am trying to add a registry key which will allow my program to

  • 0

I am trying to add a registry key which will allow my program to start when my computer boots. It seems to not be working. Here is my code to do this:

String[] execArgs = new String [2];

if ( os.indexOf( "windows" ) > -1 ) {
    File source = Methods.getJar(); //This method simply returns the full path to the running jar file.
    URL url=null;
    url=source.toURL();  
    File f = new File( URLDecoder.decode( url.getFile(), "UTF-8" ) );
    String userDir = System.getProperty("user.home");
    String path ="" ;

    if ( os.indexOf( "7" ) > -1 ) {
        path = (userDir+"/AppData/Roaming/Microsoft/");
    }

    if ( os.indexOf( "vista" ) > -1 ){
        path = (userDir+"/AppData/Roaming/Microsoft/");
    }

    if ( os.indexOf( "xp" ) > -1 ){
        path = (userDir+"/Start Menu/Programs/Startup/");
    }

    path = path.replaceAll("%20"," ");
    System.out.println("Will copy file to " +path);
    File targetDir = new File(path);
    FileUtils.copyFileToDirectory(f, targetDir);//Copy the file to appdata/roaming dir.
    String filename = source.getName();
    int last = filename.lastIndexOf("\\");
    filename = filename.substring(last + 1);
    String fullPath = path+filename;
    int l1 = filename.lastIndexOf(".");
    String fileText = filename.substring(0,l1);
    execArgs[0] = fileText;
    execArgs[1] = fullPath;
    new WinReg().exec(execArgs);  //Add a reg key to add to startup using WinReg class.
}

Here is my WinReg class:

package test;
import java.text.MessageFormat;  

public class WinReg {  
    static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";  

    void exec(String[] args)throws Exception {  
        if (args.length != 2)  
            throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");  

        String key = args[0];  
        String value = args[1];  

        String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value });  
        System.out.println(cmdLine); //get the final cmd that will be run
        Runtime.getRuntime().exec(cmdLine);  
    }  
}  

When printing cmdLine I get this:

cmd /c reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\Curren
tVersion\Run" /v "Test" /d "C:\Users\CJ/AppData/Roaming/Microsoft/Test.jar" /t R
EG_EXPAND_SZ

I tried to manually run this command, but I couldn’t figure out the syntax error it was returning.

  • 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-17T10:11:39+00:00Added an answer on June 17, 2026 at 10:11 am

    The reason cmd /c doesn’t work as present is because if the command has spaces, you must put it in quotes. If you want to use cmd /c, it is hard to format correctly, because of the nested quotes:

    rem Does not work:
    cmd /c "reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run" /v "Test" /d "C:\Users\CJ/AppData/Roaming/Microsoft/Test.jar" /t REG_EXPAND_SZ"
    

    The quotes with one set inside another confuse the interpreter.

    But, cmd /c is unnecessary: just run the command directly–

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run" /v "Test" /d "C:\Users\CJ/AppData/Roaming/Microsoft/Test.jar" /t REG_EXPAND_SZ
    

    Java runs the command as if you typed it into the command line. So, just type it like you would on the command line, as in the second code snippet above!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add registry key : My.Computer.Registry.LocalMachine.CreateSubKey(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\TESTKEY) but key is created under :
Note: I am using Delphi XE2 I am trying to add a registry key.
I am trying to retrieve some values from the registry. Here is the full
I am trying to program a simple employee registry and I want to use
When I am trying to upload the registry extension file (RXT) as Add resource
I'm trying to use RegNotifyChangeKeyValue to monitor changes of a 64-bit registry key. To
I am trying to write a vbscript utility that will add or remove several
I'm trying to add meta descriptions to 700+ products on a site I'm working
I'm trying add data triggers to the default combobox style so each text item
I am trying add picture boxes dynamically. My code is as PictureBox picture =

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.