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?

3
  • 1
    Try to echo the command instead of executing it, check if $JAVA_HOME is available from inside the script Commented Jan 30, 2020 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 Commented Jan 30, 2020 at 16:26
  • 1
    Thanks guys, it worked, you may answer it if you like? Commented Jan 30, 2020 at 16:36

2 Answers 2

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/
Sign up to request clarification or add additional context in comments.

Comments

1

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

JAVA_HOME=/path/to/java /path/to/script

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.