I am working on a Java program which will be executed on both Windows and Mac environments. Personally I'm on Windows. I have got my colleague on macOS 11.4 to test the JAR and it works, but we are trying to have a script which just saves keystrokes by avoiding having to type java -jar lfm.jar. The JAR has various tasks, the important one here is maplog, so the syntax is just java -jar lfm.jar maplog [directoryname]. So here's my very simple script maplog.sh:
#!/bin/sh
java -jar lfm.jar maplog "$@"
So you should be able to do sh maplog.sh [directoryname] and it has the same result as java -jar lfm.jar maplog [directoryname]. (Incidentally, I did test this using Bash on Windows and it worked fine.) But we are finding that on macOS, the file is reported as not existing when passed using the script, even when using absolute paths.
Here's the relevant part of my Java 11 code (with various irrelevant or error-handling stuff removed for brevity):
if ("maplog".equals(args[0])) {
String arg = args[1];
Path file = Path.of(arg);
if (!Files.exists(file)) {
System.err.println("Specified input path does not exist!");
System.err.println("Specified input path was: "+arg);
return;
} else {
System.out.println("Specified input path exists!");
System.out.println("Specified input path was: "+arg);
}
}
Here's the output when my colleague ran this code (I have only redacted their username and hostname, which I have already checked are the same and not the cause of the issue - also note that their username does not contain any spaces):
Basically, the queried path is exactly the same, but it gets a different result depending on whether you run it via the script or not.
In the end, my colleague found a modification to the script which worked
#!/bin/sh
echo $(java -jar lfm.jar maplog "$@")
This now works and correctly finds that the file exists. But I still have no clue why the original doesn't work and why this modified solution does work. Perhaps if I understood it better, I could find a better solution.
