Score:1

Why might Java code run fine manually, but not from a Shell Script?

no flag

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...

guiverc avatar
cn flag
Ubuntu 22.04 doesn't yet exist; it's currently the *development* release Ubuntu *jammy* and remains that until it reaches RC state which isn't expected until after 14 April 2022, and isn't on-topic here until release on 21 April 2022. https://discourse.ubuntu.com/t/jammy-jellyfish-release-schedule/23906 Please refer https://askubuntu.com/help/on-topic. For support issues with Ubuntu *jammy* you'll need to use a #ubuntu-next or #ubuntu+1 site (IRC, UF etc)
guiverc avatar
cn flag
If you wish to report bugs, firstly thank you for helping test the release, but please see https://help.ubuntu.com/community/ReportingBugs and use a #ubuntu+1 site such as IRC, https://ubuntuforums.org/ etc. *This site isn't tracked for ubuntu+1 or #ubuntu-next issues* Don't forget *jammy* is still in *alpha*
Pete avatar
no flag
@guiverc Thanks Guiverc! You think this could be a bug? >gulp< Man, I hope not...
guiverc avatar
cn flag
I don't know; I only scanned your text once I saw it was off-topic; however issues with Ubuntu-next (Ubuntu+1) are done on certain sites, which allow testing for issues to be confirmed, bugs filed so they can get fixed - this is not a Ubuntu+1/Ubuntu-Next site ; this site only supports *standard* products, ie. released and not ESM/EOL, nor *development*.
hr flag
*one* possibility is that `mvn` is aliased to something in your interactive shell (aliases aren't expanded inside scripts, by default)
fuzzy drawings avatar
tj flag
Check output of `find "$HOME" -type f -name 'log4j2.xml'` to see if your paths are correct.
Score:2
tj flag

I think there may be a problem with the path you are cd'ing to in the script.

Looking at your command prompt when mvn test worked:

me@UbuntuV2:~/home/me/path/to/Java/Code$ mvn test

The leading ~ indicates $HOME precedes the displayed path, which would mean the full path is actually /home/me/home/me/path/to/Java/Code. This is not the same location as /home/me/path/to/Java/Code in your script.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.