-1

I have a directory C:\study\runnable with a subdir conf, containing a batch file e.bat and a test.jar file and a configuration file configuration.conf.

test.jar works if it reads configuration.conf. When I launch test.jar it works.

I make a link with the e.bat file that has this inside:

mklink "%~dp0collegamento_test.jar" "%~dp0test.jar"

It creates collegamento_test.jar adjacent to test.jar, and if I launch it with double click, it works. But if I move collegamento_test.jar to Desktop and I launch it, it doesn't work. It starts the application, but the application doesn't find conf directory, so it can't read configuration file.

Do I have to insert something else to the mklink command?

I would like to insert the move command in bat file under the mklink command, and I want lo launch test.jar by the link present in Desktop.

Compo
  • 36,585
  • 5
  • 27
  • 39
francesca
  • 1
  • 2
  • I'm not seeing the need to create it adjacent to the original then wanting to `move` it. Why not use ```@%SystemRoot%\System32\mklink.exe "%UserProfile%\Desktop\collegamento_test.jar" "%~dp0test.jar"``` instead? Also, why create a junction? Have you not considered creating a shortcut to `C:\study\runnable\conf\e.bat` directly on the `Desktop` and naming it `test.jar.lnk`? At least that way you can specifically define the working directory in the `Start in:` property of the shortcut file as `C:\study\runnable\conf`. – Compo Aug 28 '23 at 17:49
  • The Java application obviously expects that the __current directory__ on executing `java.exe` with `collegamento_test.jar` or `test.jar` is the directory containing the JAR file. That is a not good design because of the process starting `java.exe` by calling the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) defines with value of function parameter `lpCurrentDirectory` the current directory for Java application. – Mofi Aug 29 '23 at 06:15
  • There is either modified the Java application inside the JAR file to determine itself in which directory the JAR file is and reference all other files and folders in same directory using the directory path of the JAR file or the batch file is extended with the command __CD__ to change the current directory of `cmd.exe` process to the directory containing the JAR file and the other files and folders. `cmd.exe` calls `java.exe` with a null pointer for `lpCurrentDirectory`, except there is used the command __START__ to run `java.exe` with the option `/D` to define a different current directory. – Mofi Aug 29 '23 at 06:19
  • BTW: Double clicking on a batch file results in `explorer.exe` calling `CreateProcess` to run `%ComSpec%` with the option `/c` and the fully qualified batch file name appended as additional argument for `%SystemRoot%\System32\cmd.exe` with `lpCurrentDirectory` pointing to a string containing the full directory path. In other words `explorer.exe` sets by default the batch file directory as current directory for `cmd.exe`. The usage of __Run as administrator__ usually results in using `%SystemRoot%\System32` as current directory and many batch file fails for that reason. – Mofi Aug 29 '23 at 06:25
  • See: [Why does 'Run as administrator' change (sometimes) batch file's current directory?](https://stackoverflow.com/a/31655249/3074564) Running a batch file with a double click from a network resource using the UNC path results in `cmd.exe` itself changes the current directory to `%SystemRoot%` before starting the processing of the batch file, see [CMD does not support UNC paths as current directories](https://stackoverflow.com/a/45072396/3074564). No application/script should expect ever that the current directory is always the application/script directory. – Mofi Aug 29 '23 at 06:29
  • PS: Most of the properties of a shortcut file define the values passed by `explorer.exe` to `CreateProcess`. The property __Start in__ is passed with `lpCurrentDirectory` to `CreateProcess` as it is also done by the Windows *Task Scheduler* with the scheduled task property __Start in__. The __Start in__ directory path of a shortcut or a scheduled task must be always without `"` even on containing a space or one of these characters ``&()[]{}^=;!'+,`~`` while application name and argument strings must be enclosed in `"` on containing a space or one of these characters ``&()[]{}^=;!'+,`~``. – Mofi Aug 29 '23 at 06:32
  • if I insert in my batch: @%SystemRoot%\System32\mklink.exe "%userprofile%\Desktop\test.jar" "%~dp0test.jar" doesn't work.. can you please make me a sample of text that I have to have inside my batch ? I need to have my batch near to test.jar, so not on my desktop I still don't undestrand why if I make a link by windows mouse right click,and I move it on desktop, it works (when I launch it with double click it find conf directory and so on) If I make a link with mklink and I move it on desktop, it doesn't works.. Where is the difference?? – francesca Aug 30 '23 at 04:59

0 Answers0