1

I want to make a windows shortcut, which opens javaw.exe at a variable destination. This destination is the content of my system environment variable %MY_OWN_JAVA_HOME%.

I tried to do:

  1. right click in folder (using windows explorer)
  2. New => Shortcut
  3. destination: %MY_OWN_JAVA_HOME%\bin\javaw.exe -cp ......
  4. Next, Finish

The problem is, that the environment variable is calculated at creation time. When I open the settings of this environment variable, then the current value of the variable is used.

How do I keep the variable as the destination of my shortcut?

  • Put %MY_OWN_JAVA_HOME%\bin\javaw.exe -version in a batch file and run the batch file from the shortcut. – DavidPostill Sep 14 '15 at 11:39
  • @DavidPostill thx, that works. But it always opens a cmd-window. If I use start %MY_OWN_JAVA_HOME%\bin\javaw.exe -version in the batchfile, then the cmd-window will close right away. And I am minimizing the cmd-window with my shortcut-settings. Still I would prefer not to open that second window. – slartidan Sep 14 '15 at 11:46
  • Try start /B Start application without creating a new window` – DavidPostill Sep 14 '15 at 11:50
2

You should still be able to create a shortcut with environment variable using a COM Object from VBScript or powershell. The method "CreateShortcut" (more details also for arguments here) is capable of that. While originally a vbscript solution you can call this from a powershell prompt like this:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\Public\Desktop\My Java Shortcut.lnk")
$Shortcut.TargetPath = "%SMS_ADMIN_UI_PATH%\javaw.exe"
$Shortcut.Arguments = "-cp"
$Shortcut.Save()

Please note that the arguments do not go in the target path. Also it might not work if your environment variable contains spaces. The upside of this is that you do have a real shortcut not some wrapper, the downside is, that you can never alter this shortcut via it's properties dialog. As soon as you do this the variable gets replaced again.

If you want a wrapper I would also suggest a vbs file which is completely silent. Create a new text file and paste the line

CreateObject("WScript.Shell").Run """%MY_OWN_JAVA_HOME%\bin\javaw.exe"" -cp", , False

in there and save it as "runmyjava.vbs" and create the shortcut to that file and it wont have an black popups non even for a split second. Note the extra "" in front after the path to the executable. Those are only needed if the environment variable contains spaces.

1

One solution is to use a .lnk shortcut file that has been created in Windows XP. Those shortcuts will keep your variables in the shortcut destination - even if you change the destination.


Another solution is, as suggested by @DavidPostill in the comments, to use a batch file like this as shortcut target:

@echo off
start "" "%MY_OWN_JAVA_HOME%\bin\javaw.exe" %*

Then select "Minimize" in the shortcut options.

This solution works fine, but opens a minimized cmd-popup for a second.

-1

I want to suggest another solution, that works in Windows Vista, although I don`t know if it works in newer Windows versions: In my experience, write protecting the .lnk file will always prevent target/destination variables from being expanded. They are often expanded when the .lnk is created (or when it's used), but they can be reinserted and then maintained by write protecting the .lnk before you use it (the next time).

  • This answer seems incomplete. – Ramhound Apr 14 '16 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.