1

I've got a Java installer which deploys a binary to "ProgramFiles"\myapp. I'd like to make it accessible from the command line.

Under Linux, the binary is deployed to /usr/local/bin/. Under Mac, I plan to deploy the binary to /usr/bin/.

How do I make a single command line binary accessible under Windows? Obviously I could send the binary to "WindowsDir"\System32... That would not be a clean way to do it though.

I think updating the user PATH environmental variable is a good option. How would I make it persist after reboot though?

edit: I don't want to hear it can't be done as has been said in two answers so far. That's a wrong answer and should be voted down....

C:\>echo %PATH%
C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\WINDOWS\system32
C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\

Obviously Apple figured it out because QucikTime is in my path.

AdamOutler
  • 870
  • 4
  • 13
  • 29

4 Answers4

2

Try this:

set <environment variable>=<value>
Dima
  • 1,788
  • 2
  • 22
  • 30
1

I don't believe that java can actually modify environment variables. See this post about that: How do I set environment variables from Java?

The not clean answer is to use ProcessBuilder. As the OP pointed out, calling the windows set command like this set path=<your path>;%path% only modifies the variable in your process's environment.

A better way is to modify the registry with the reg command. The correct registry key is outlined in this Wikipedia Page: http://en.wikipedia.org/wiki/Environment_variable

Through the Windows Registry this is done changing the values under HKCU\Environment (for user specific variables) and HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (for System variables).

Also, if you aren't targeting Windows XP and earlier, you can try the setx command which was added in Windows Vista: http://technet.microsoft.com/en-us/library/cc755104(WS.10).aspx

Community
  • 1
  • 1
Jon7
  • 7,165
  • 2
  • 33
  • 39
1

This is how I went about it. I am adding Program files\Heimdall to the registry...

             String GetReg[]={"reg", "query", "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "/v", "Path"}; 
             String RegVal="";
             try {
               RegVal =Shell.sendShellCommand(GetReg).split("   ")[2].replace("\n", ""); 
             } catch (ArrayIndexOutOfBoundsException e){
               RegVal = Shell.sendShellCommand(GetReg).split("    ")[2].replace("\n", ""); 
             }
             if ((RegVal.contains("C:\\")) || (RegVal.contains(";%SystemRoot%"))){
                 String RegPATH=RegVal+ProgramFiles+"\\Heimdall\\;";
                 Log.level3(RegVal);
                 Log.level2("Attempting to insert" + RegPATH + "into Registry");
                 String RegCommand[]={"reg", "add","HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "/v", "Path" ,"/t" ,"REG_EXPAND_SZ",  "/d", "\""+RegPATH+"\"" , "/f"};
                 if (! RegVal.contains(ProgramFiles+"\\Heimdall\\")){
                     Shell.sendShellCommand(RegCommand);
                 }

             }
AdamOutler
  • 870
  • 4
  • 13
  • 29
0

The path variable like everything else in windows is managed in the registry. Reference this post for the appropriate keys. Programmatically adding a directory to Windows PATH environment variable. You will need to write a native function. As always note this will limit the platform independence of your application. If this is not your cup of tea there exists a JNI based java registry editor called jregistrykey

Community
  • 1
  • 1
nsfyn55
  • 14,875
  • 8
  • 50
  • 77
  • Irrelevant. How can that be accessed from within Java? – AdamOutler Jun 30 '11 at 01:24
  • Irrelevant? Did you read the answer? This is how you add a binary to the path in windows. Its no different than /usr/bin on linux which also on the path. Nothing in your question said "How do I update the environment variable from my installer" Maybe phrase your question better or write a native function that calls the windows API to edit the registry. The appropriate keys are referenced in this post - http://stackoverflow.com/questions/1919125/programmatically-adding-a-directory-to-windows-path-environment-variable – nsfyn55 Jun 30 '11 at 01:48
  • I still attest without a doubt that my post answered your original question my edit happy friend. – nsfyn55 Jun 30 '11 at 01:49
  • This is an INSTALLER. Not a installed app. I need an installer method as stated in the title and the post. – AdamOutler Jun 30 '11 at 01:54
  • no. it did not. The only editing I did was after the "edit:" – AdamOutler Jun 30 '11 at 01:55
  • Man... do you speak English???? Nothing in your question indicates that you want to edit the Path variable from your java installer. At any rate I have provided you the answer...Twice. Once in a comment and twice in my modified response. Good Luck – nsfyn55 Jun 30 '11 at 02:03
  • Really? Do you understand what a installer does? What, should I pop up a dialog box to say "hey, perform this action now to complete installation"? – AdamOutler Jun 30 '11 at 02:17
  • @nsfyn55 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/994/discussion-between-adamoutler-and-nsfyn55) – AdamOutler Jun 30 '11 at 02:17
  • ughhh....dude I'm just trying to help you get answers on SO. You just need to phrase your question with a bit less ambiguity. There was no way for me to know that you wanted to edit %PATH% from your java installer programmatically. BTW Did what was the final answer? Is there an easier way to do this beyond editing the registry? – nsfyn55 Jun 30 '11 at 12:22
  • Command line: reg is the answer. The user variables are stored in the registry. – AdamOutler Jun 30 '11 at 14:34