Environment variable in .lnk shortcut
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Comments
-
slartidan over 1 year
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:
- right click in folder (using windows explorer)
New
=>Shortcut
- destination:
%MY_OWN_JAVA_HOME%\bin\javaw.exe -cp ......
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?
-
DavidPostill over 8 yearsPut
%MY_OWN_JAVA_HOME%\bin\javaw.exe -version
in a batch file and run the batch file from the shortcut. -
slartidan over 8 years@DavidPostill thx, that works. But it always opens a
cmd
-window. If I usestart %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. -
DavidPostill over 8 yearsTry
start /B
Start application without creating a new window`
-
Ramhound about 8 yearsThis answer seems incomplete.
-
Mingye Wang over 3 yearsIt may be a good idea to provide an example of an XP lnk, so that someone could take it apart with liblnk and figure out how to create it without XP. Maybe I should get a VM and do it...