Windows Using Symbolic Link in Shortcut property - windows

When the Java runtime is installed on a Windows PC, a folder C:\ProgramData\Oracle\Java\javapath is created. It contains javaw.exe a SYSMLINK (not a .lnk file) to javaw.exe for the current verion of Java, such as C:\Program Files\Java\jre1.8.0_51\bin\javaw.exe.
This link can be used from the command line, and also in the registry (handy to add right click actions on files in explorer).
However if I create a shortcut (.lnk) file and set the properties/target to run a java program, I have to use the proper path to javaw, for example:
"C:\Program Files\Java\jre1.8.0_51\bin\javaw.exe" -cp S:\JavaClasses\production\WindowsTools Clippo /NS
If I use the SYMLINK file, when I double click on the .lnk shortcut a dialog asks me which program I would like to use to open the file.
Keeping the proper path in the shortcut is a pain, because when a new version of Java is installed and the old version removed, I have to update the links.
I could make my own hard link, and then I would only have to change that after updating Java, but is there a way to use the links provided by Oracle?
I realise I could use cmd /C, but I don't want a console flashing up (hence I am using javaw.exe and not java.exe

It appears that as well as the SYMLINK, a direct exe is placed in C:\Windows\System32 so I can just use:
C:\Windows\System32\javaw.exe -cp S:\JavaClasses\production\WindowsTools Clippo /NS
as the target in the .lnk shortcut file.
EDIT
Having read Harry Johnston's comment below this is not an ideal answer, thus if anyone knows the answer to the original question of using a SYMLINK, I'd be interested to know.

Related

Change location to shortcut location

I am launching a script with a powershell shortcut (C:\Windows....\powershell.exe -file 'D:\powershell\script.ps1').
Is there a way to make script change its current location to location of said shortcut?
Example: Script itself is in D:\powershell\ and the shortcut is in C:\Work\Project1. I need the script to cd to "C:\Work\Project1\".
Thanks
You can make a shortcut that starts in whatever directory it's located in. All you have to do is modify the "Starts In" property of the shortcut, and blank it out. That's right. Set it to nothing.
Then, when you want to invoke the shortcut, navigate to the folder (directory) where it's located before invoking it. I use this technique for a shortcut that launches powershell but doesn't launch a script. I haven't tested it with a shortcut that launches a script.
Walter Mitty's helpful answer provides an effective solution, provided that the shortcut file is opened from either the Desktop or File Explorer.
This answer provides background information.
You must configure the desired working directory as part of the shortcut file, because the script you invoke knows nothing about the shortcut that invoked it.
Therefore, to configure a specific working directory (e.g., C:\Work\Project1), specify it in the shortcut file's Properties dialog in the Start in: field.
In case you want to update a shortcut file's (*.lnk) working directory programmatically, use the technique from this answer with the .WorkingDirectory property.
Note: In both cases, only an absolute path can be configured as the working directory: File Explorer only allows you to enter an absolute path anyway, and while the programmatic method allows you to assign a relative path, it is instantly resolved to an absolute one, relative to the shortcut's location.
To make the shortcut's own directory the working directory, you can blank out the Start in: field / .WorkingDirectory property, but note the limitations:
Only works when such a shortcut is either opened from either the Desktop or from File Explorer.
By contrast, opening it from the taskbar or (pre-Windows 10 only) the Start Menu, the working directory is $env:windir\System32 (typically, C:\Windows\System32).
If the shortcut targets an application (rather than a document), as in this case, that application invoked by the shortcut - including cmd.exe and powershell.exe - starts in the configured / implied working directory.
Caveat re cmd.exe: If the working directory is specified as as a UNC path, cmd.exe won't be able to change to that directory; as a workaround, use a path with a mapped drive instead (but, obviously, that drive must be mapped at the time the shortcut is opened).
You can use the TargetPath property of the shortcut (maybe do a get-childitem $psscriptroot where name is like scriptname and extension is like .lnk) with the set-location cmdlet
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6

Command-line equivalent of "start in" field on windows shortcuts

I'm in Windows 7. I have an executable. I put it somewhere. I put this location in my path. Now I can start it from anywhere using cmd. I have a different location where I work containing files the .exe will process. I shift right-click to open cmd in the work location. I can run the exe, but the exe starts in its own location and can't find the files. If I were to create a windows shortcut to it, I would get a "start in" field, which would work. But I do this a lot from many different locations and I don't want to create a shortcut in each location. How do I do this on the command line without creating a shortcut? It would be great if I could just run something like
progam /startin .
What is the actual syntax?
#echo off
setlocal
pushd "c:\where\you\want\to\start"
programname
popd
This is the basic structure.

Windows CMD: How to create symbolic link to executable file?

My goal is to add a few executables to my PATH (for example, chrome), so that I can call
> chrome
from the command prompt and it will launch Chrome.
I know I could add Chrome's containing directory to my path (set PATH=%PATH%<chrome_path_here>;), but since I have a few executables I want to add, I'd rather make a new bin directory that contains symbolic links to the actual executables and just add that single directory to my PATH.
The Chrome executable is located at
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
So I tried
> mklink chrome.exe "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
That successfully creates a symbolic link for the files (says so in output, and upon examining with > dir). I know my PATH is set up correctly, b/c when I run > where chrome it finds my new symbolic link.
However, when I try to execute chrome with my new link, nothing happens. A new empty window should appear, but nothing happens. No error message in the command prompt or anything.
What am I doing wrong? Am I misunderstanding symlinks in Windows? This is the approach I use in Linux all the time, but I'm new to Windows Cmd.
Thanks!
Most programs will not run from places other than they install location - which is exactly what happens when you try to run it from symlink.
It would be much easier to create CMD/BAT files in that folder with matching names which will launch programs from locations you want:
REM chrome.cmd
start /b cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %*
With Windows 7 I confirm that symlinks do not work, are simply ignored as reported in the original question.
As Harry states in his comment, shortcuts do work, and to me are simpler and easier than writing a separate script for each new command I want to enable under CMD.
He states that you need to add .lnk to your PATHEXT variable in order to do this. I affirm that this does work, and with .lnk added to PATHEXT I can simply enter the name portion of the shortcut to run the command. For example if my shortcut is named "sublime.lnk" and PATHEXT includes .lnk, I can execute the link with the simple command "sublime". Nice!
As an alternative I found that PATHEXT need not be modified if I simply type in the full name of the shortcut, including the .lnk, at my CMD prompt. E.g., I created a shortcut named "sublime.lnk" under %HOMEPATH%/bin, pointing to "C:\Program Files\Sublime Text 2\sublime_text.exe".
Now by placing %HOMEPATH%\bin in my %PATH% I can run sublime via the command "sublime.lnk".
Either of the above are the best way I know of giving access to various commands from around Windows' filesystem from a CMD prompt. I'm not a Windows expert though, and welcome a better or more standardized solution to this problem.
P.S.: I just found out the hard way that you need to ensure the "Start in:" property of any shortcut you use in this fashion is blanked out, or your program will not start in the directory you invoke the shortcut from.
P.P.S.: On a related note, I discovered how to run Windows Explorer (or its replacement) on the directory your CMD session is logged in to: start ..

Adding a Application specific paths, so it works from the command line in Windows

Following the guide from Microsoft,
http://msdn.microsoft.com/en-us/library/ee872121(VS.85).aspx , I am able to get my program to be able to make a program resolve the dynamic libraries that are required in order for it to work.
So I add a value with the full name and path to my executable, and add subkey to this entry (named path) with the full path the directory of the DLL files.
And magic. It works. I go the start menu, and types myprogram.exe and it starts up and is now able to locate the dll files correctly.
However, if I start the command prompt using the command cmd.exe, and then try to run myprogram.exe is not able to resolve the DLL's anymore. For some reason the command prompt do not seems to respect/read the values of the registry when it is set under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
Any suggestion to how I can get this behavior to work from within the command prompt as well as from the start menu?
It is correct. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths is used by ShellExecuteEx and not by CreateProcess. So not all programs will use the settings from App Paths of your application. If you want to define PATH for cmd.exe you can either use subkey of App Paths with the name cmd.exe or use an old %SystemRoot%\System32\autoexec.nt file to modify PATH environment variable.
It's also possible to use "START /WAIT app.exe" from command line which uses ShellExecuteEx.
I'll give it my best shot.
First, notice that both the cmd & the run\start menu options are running everything in C:\WINDOWS\system32.
If you're dll was there then it would work.
if you don't want to put it there, you can change the "environmental variables" by clicking right mouse button on "my computer"-> "properties" -> "Advanced" -> "environmental variables".
good luck.

How do you add Start->Run shortcuts in Windows XP?

Does anyone know how you setup new commands to launch an application from the Start->Run box?
ie. you can type "firefox", "winword" or "excel" into the Run box and those applications will open even though they're not in the system path, but others won't. For example, with Firefox, there's no files named firefox.* in any of my system path directories:
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Subversion\bin;c:\Program Files\Microsoft
SQL Server\90\Tools\binn\
So there must be some way of linking applications with this path to create a shortcut command, maybe in the registry?
For example, I want to be able to launch Google Talk from the Run command, but typing the executable's name "googletalk.exe" doesn't do anything, which makes sense because it's not in the system path, but neither is firefox.exe, and typing "firefox" works.
Does anyone have any ideas?
From http://commandwindows.com/runline.htm:
Adding applications to the Path
Alternatively, the Registry can be
edited to explicitly contain the path
to the desired executable file or
files. The Registry key involved
isHKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App
Paths
Create a new sub-key with the name of the executable file that you
wish to add to the path. e .g.,
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App
Paths\somefile.exe
In this new key, add a string variable named "Path" containing the
value of the the path to your new
executable file, e.g., C:\Program
files\newprogramfolder\
The new key will already have an empty variable (Default). Edit it to
have the string value of entire
address of the new program executable , e.g., C:\Program files\newprogramfolder\somefile.exe
3 steps.
Create a shortcut to the foo.exe you want to run (foo.lnk)
Copy the foo.lnk to your C:\windows directory.
Run foo.exe by simply typing "foo" in your run dialog. ("start foo" in cmd prompt works too)
You could also use an application launcher like Launchy, Slickrun or Executor. That way you wouldn't have to mess with the registry. I use Executor and I can just select "send to -> executor" to create a shortcut for any app (or folder or whatever).
Run works with search paths, which include the Path environment variable and some others. There might be more information in the MSDN, give me a moment and I'll look it up.
I put a link to the application I want to launch in the "c:\windows" folder, and I use it all the time. You can also rename the link file, of course, to speed up things: for example, I type w, x or pp to launc Word, eXcel or PowerPoint.

Resources