Hi have a frustrating problem: On my Ubuntu 22.04 machine, I have some Java/Maven code which I can run manually from the command line:
me@UbuntuV2:~/home/me/path/to/Java/Code$
me@UbuntuV2:~/home/me/path/to/Java/Code$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< MyCode.java >--------------------
...code runs fine...
This is great, but I need to run this same code from a shell script:
#!/bin/bash
echo "Running the script!"
cd /home/me/path/to/Java/Code
mvn test
echo "Script finished."
What's weird is that the script can launch the Java code, but the script-run code throws an exception:
me@UbuntuV2:~/home/me$
me@UbuntuV2:~/home/me$ ./runScript.sh
Running the script!
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< MyCode.java >--------------------
2022-03-02 12:29:44,899 main ERROR Unable to access file:/home/me/path/to/Java/Code/src/test/resources/log4j2.xml java.io.FileNotFoundException: /home/me/path/to/Java/Code/src/test/resources/log4j2.xml (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
...etc...
This is superweird, right? When run through the shell script, the Java code suddenly can't access a "log4j2.xml" file. ("No such file or directory") But the file is there, in place, and the code has no issues opening it when I run the code manually.
So what is different when I run the code manually? I've racked my brains trying to think of what could be different. I've done all of the following:
- Verified that user
me
manually runs the code from a bash shell, just like the script does.
- Run the script as user
me
, the same user who can run the code manually
- Verified that the script runs as
me
by adding a whoami
command in the script to check
- Used a
pwd
command to verify that the script run the code from the correct directory.
- Set the script's PATH variable to be the exact same Path as user
me
using the command export PATH=$PATH:/usr/local/sbin:...
in the script
- Used the visudo command to ensure that user
me
has unlimited access to all files on the Ubuntu
- Made sure the user
me
owns the script and has execute rights
- Yelled and swore at the Ubuntu a lot
What else could the problem be? I didn't write the Java code, so maybe the code itself refers to this log4j2.xml
file by relative paths, not absolute paths...? But if that's the case, why does the code work when manually run?
I know this is an open-ended question, but what else might be different between the script- and the human-initiated versions of running this code. I've gotta be missing something...