0

I have a bash script that look like below. All I am doing here is trying to run a java program passing in classpath and other inputs.

[guest@server1]# cat runJava
#!/bin/bash
JARS=/home/guest/lib/*
CONFIG=/home/guest/AnalyticServer.properties

echo JARS=$JARS
echo CONFIG=$CONFIG
echo "java -cp \"$JARS\" com.test.ASDataSourceClient DELETE -c \"$CONFIG\" -n test_ds"
java -cp "$JARS" com.test.ASDataSourceClient DELETE -c "$CONFIG" -n test_ds

The java program runs perfectly fine when run directly on command line/terminal. I even tested running all the commands from above script on terminal directly(one by one) and verified that java program runs fine. However, when I launch these commands via script, I am having issue. It doesn't even seem to execute echo "java -cp .." command properly.

Could anyone advice what I may be missing.

[guest@server1]# bash runJava
JARS=/home/guest/lib/*
CONFIG=/home/guest/AnalyticServer.properties
" -n test_dsaSourceClient DELETE -c "/home/guest/AnalyticServer.properties
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/ASDataSourceClient
Caused by: java.lang.ClassNotFoundException: com.test.ASDataSourceClient
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.test.ASDataSourceClient.  Program will exit.
1
  • Your echo is fine and should be showing. The error message is from java. How are you running the scripts? Commented Feb 3, 2014 at 18:26

4 Answers 4

3

Due to the way globbing works, JARS=/home/guest/lib/* won't do what you want.

Instead, you can use either

JARS=(/home/guest/lib/*)  # Put all the jars in an array 
IFS=:                     # Set the field separator to :
java -cp "${JARS[*]}" ... # Join all the array elements on the field separator

or less obscurely

JARS=""
for jar in /home/guest/lib/* # Loop through all the jar files
do
    JARS="$JARS:$jar"        # Append the jar file to the variable
done 
java -cp "$JARS" ...

This will make the JARS variable contain a colon separated list of jar files.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also do string contatenation with: JARS+=":$jar" -- not sure what version of bash that requires. Also, trim the leading colon with "${JARS#:}"
1

It's not enough to point to the directory containing JARs. You need to name them explicitly, e.g.

JARS="/home/guest/lib/spring-beans-3.0.5.jar:anotherlibrary.jar"

1 Comment

Thanks, I would try that method but don't understand why * wouldn't get expanded to look for all the jar files in that directory.
0

I notice this odd line in your script output:

" -n test_dsaSourceClient DELETE -c "/home/guest/AnalyticServer.properties

That indicates $CONFIG ends with a \r character. Are you editing this script in a Windows text editor? Remove carriage returns in your script with dos2unix

Comments

-1

Try this

#!/bin/bash

JARS=/home/guest/lib
CONFIG=/home/guest/AnalyticServer.properties

echo JARS=$JARS
echo CONFIG=$CONFIG

for file in `ls $JARS/*.jar`;
do
    classpath=$classpath:$file
done

cmd="java -classpath $classpath com.test.ASDataSourceClient DELETE -c $CONFIG -n test_ds"
echo $cmd
exec $cmd

1 Comment

There are two problems with this answer: iterating over the output of ls, and relying on the word-split expansion of a parameter to be equivalent to the original command line. Both will break when they involve file names that contain whitespace.

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.