0

I was trying to execute my java program through shell script so I wrote:

java -jar $(pwd)"/test.jar"

It worked flawlessly but when I turned to the code below:

PATH=$(pwd)"/test.jar"'
java -jar $PATH

Then I got an error: "Run.sh: 3: java: not found" (Running on Ubuntu)

I have very little experience in shell script so please let me know what's wrong with it. Thanks.

3 Answers 3

4

PATH is a special environment variable which the shell uses to find executables. You've changed PATH to point at test.jar, so now the shell can't find java.

Call your variable something else.

Example:

LIB_PATH="$(pwd)/test.jar"
java -jar ${LIB_PATH}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but since we're on the topic of special env vars, might as well use $PWD rather than call the external binary
1

The value in $(PWD) depends on the directory the script is called from (print working directory). If you call the script from another directory, than the one your jar-files resides in, you'll get the wrong path. And you changed the search path of the SHELL, that will prevent the shell from finding any other binary e.g. java.

1 Comment

The problem is that his shell no longer knows there the java binary is because he overwrote the PATH variable that it uses to find it.
1

PATH is system-reserved variable, that define the way where your system should look to find the executable (in your case java). Therefore you shouldn't use it in your code as variable to your test.jar .

In my opinion, your code should be something like:

#!/bin/sh

PROGPATH='/path/to/your/test.jar'
JAVAEXEC=`which java`
JAVAPARAMS='-j'

GLOBALPATH="$JAVAEXEC $JAVAPARAMS $PROGPATH"
echo $GLOBALPATH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.