0

I've a weird issue coming on a new server that I switched (switched from ubuntu 18_04 to Red hat linux). One of our script started throwing an error, but if I copy paste the same command on command shell, it works fine.

Shell is bash and the command is

g++ -Wall -fPIC  -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -o Test.so Test.cpp

so it works fine if ran manually, but throws the following error while ran inside any script.

In file included from Test.cpp:2:
Test.h:2:10: fatal error: jni.h: No such file or directory
 #include <jni.h>
          ^~~~~~~
compilation terminated.

I tried running script by all possible forms, removed all the other commands but this one and even this simplest script is throwing the same error that I posted above; test.sh

#!/bin/bash
g++ -Wall -fPIC  -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -o Test.so Test.cpp

So my question is that if a command runs without any issue on command prompt/bash, (where it does find all paths for JAVA_HOME, finds jni.h and generate the .so file) then why does it fail inside a script?

Naumann
  • 357
  • 2
  • 13
  • 1
    Try to echo the command instead of executing it, check if $JAVA_HOME is available from inside the script – Francesco Gasparetto Jan 30 '20 at 16:23
  • 1
    I'm guessing your variable is not exported. In your shell, `declare -p JAVA_HOME` won't show `-x` and `env` won't list it. If so, see [this question](https://stackoverflow.com/questions/815742/access-a-variable-in-a-bash-script) – that other guy Jan 30 '20 at 16:26
  • 1
    Thanks guys, it worked, you may answer it if you like? – Naumann Jan 30 '20 at 16:36

2 Answers2

1

Thanks for the comments from franzik and that other guy, so it was in issue with exporting JAVA_HOME, I added the following in my scripts to make them work.

export JAVA_HOME=/path/to/jvm/
Naumann
  • 357
  • 2
  • 13
1

Launch the script passing JAVA_HOME value to its ambient, like this

JAVA_HOME=/path/to/java /path/to/script
Francesco Gasparetto
  • 1,819
  • 16
  • 20