1

I'm installing java8 and tomcat9 like this:

brew install adoptopenjdk/openjdk/adoptopenjdk8 --cask
ln -s $(/usr/libexec/java_home -v 1.8) /opt/homebrew/opt/openjdk 
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' >> ~/.zshrc

brew install tomcat@9 --ignore-dependencies
brew services start tomcat@9

I export the location of java to a environmentan variable called JAVA_HOME stored in my user ~/.zshrc file because catalina.sh needs the environmentan variable JAVA_HOME setted to work.

If i start catalina.sh using terminal, works perfectly, but brew services start tomcat@9 doesn't work because the launchctl service ignores the environmentan variable called JAVA_HOME stored in my user ~/.zshrc file. So it is ignoring the ~/.zshrc file.

In Mac OSX Catalina this worked perfectly, but it's failing in Monterey

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • It could be calling `catalina.sh` or similar which probably is a Bash script. Try setting env on `.bashrc` – LMC Mar 17 '22 at 19:27
  • @LMC where is bashrc? btw take in mind that catalina.sh works perfectly if i execute it from terminal, it uses the variables stored in .zshrc. Are you sure that .bashrc whould work? – NullPointerException Mar 17 '22 at 19:29
  • it will work but will not read .zshrc in my opinion. Run `ls -la` on your home to get the MacOS name of `.bashrc` – LMC Mar 17 '22 at 19:30
  • @LMC .bashrc doesn't exist in my home – NullPointerException Mar 17 '22 at 19:32
  • ~/.bash_profile could work also – LMC Mar 17 '22 at 19:33
  • @LMC doesn't exist neither – NullPointerException Mar 17 '22 at 19:34
  • set the export in env.sh at the same directory of catalina.sh, create it if it does not exist – LMC Mar 17 '22 at 19:36
  • @LMC I tryed creating .bashrc and creating env.sh and neither works... – NullPointerException Mar 17 '22 at 19:41
  • from [catalina.sh source code](https://github.com/apache/tomcat/blob/main/bin/catalina.sh): `# Do not set the variables in this script. Instead put them into a script # setenv.sh in CATALINA_BASE/bin to keep your customizations separate.`. Sorry, it's setenv.sh instead of env.sh – LMC Mar 17 '22 at 19:53
  • @LMC but the problem is not with catalina, it works perfectly executed from terminal, the issue is with launchctl plist from brew services start which is ignoring the environmental variables. If i use other thing different from catalina, it will have the same error. I think I should find a better solution. – NullPointerException Mar 17 '22 at 19:56
  • setenv.sh should do the same .zshrc does. It's not a catalina.sh replacement. Works from terminal since your login session is giving context which brew probably ignores. – LMC Mar 17 '22 at 19:57
  • @LMC I did it, same error, sentenv.sh seems to be ignored too – NullPointerException Mar 17 '22 at 19:59
  • What's the error? `# JRE_HOME Must point at your Java Runtime installation. # Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME # are both set, JRE_HOME is used.` – LMC Mar 17 '22 at 20:02
  • @LMC the error is that the service is not starting because JAVA_HOME is not set for the service – NullPointerException Mar 17 '22 at 20:08

1 Answers1

0

This worked for me on Monterey:

echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zshrc
echo 'launchctl setenv JAVA_HOME $JAVA_HOME' >> ~/.zshrc
. ~/.zshrc
brew services run tomcat@9

See this answer if you want it to persist across reboots, not an issue for me as I rarely reboot and prefer to start Tomcat manually.

Kamal
  • 415
  • 2
  • 10