6

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?

3
  • Put %MY_OWN_JAVA_HOME%\bin\javaw.exe -version in a batch file and run the batch file from the shortcut.
    – DavidPostill
    Commented Sep 14, 2015 at 11:39
  • 1
    @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
    Commented Sep 14, 2015 at 11:46
  • Try start /B Start application without creating a new window`
    – DavidPostill
    Commented Sep 14, 2015 at 11:50

4 Answers 4

6

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 = "%MY_OWN_JAVA_HOME%\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 not 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.

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.

1
  • 1
    It 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... Commented Dec 3, 2020 at 8:46
0

Build a shortcut where 'Target' is 'cmd /k' running 'start "" /B' to run your application.

The command processor (cmd) will resolve the environment variables and close once Start completes. Start will run the application "without" a window (though it still flashes for me).

Notes:

  • Cmd wants a single argument for the program (start) and the program's arguments
  • Start wants separate arguments for the program and the program's arguments
  • %CD% will resolve to the shortcut's 'Start in' path. Clear 'Start in' for %CD% to resolve to the path of the shortcut.
  • If the Target command line is too long for Explorer/WshShell you will need to build the shortcut with another library (e.g. liblnk) or fall back to calling a separate batch file.
cmd /k "<<START COMMAND>>"

cmd /k "start "" /B "<<PROGRAM>>" <<PROGRAM ARGUMENTS>>"

cmd /K "start "" /B "%MY_OWN_JAVA_HOME%\bin\javaw.exe" %*"
3
  • Suggestion - You could improve your answer by citing a reference to this. Commented Feb 14, 2023 at 23:35
  • I agree for the Target value maximum length. Unfortunately, the references I found seemed more confusing. Were you referring to that or something else (i.e. what is "this")?
    – somann
    Commented Feb 15, 2023 at 0:34
  • 1
    this as in your use of the command parameters. So that readers know what /k and /B are meant to do. A reference gives your answer authority. Commented Feb 15, 2023 at 3:25
-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).

1
  • 1
    This answer seems incomplete.
    – Ramhound
    Commented Apr 14, 2016 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .