Reputation: 977
I am new to writing shell scripts and was trying to write a small script to run a class file in a maven project using following shell script :
function cleanup() {
kill ${SERVER_PID} ${CLIENT_PID}
rm -f cp.txt
}
trap cleanup EXIT
mvn test dependency:build-classpath -Dmdep.outputFile=cp.txt
CLASSPATH=$(cat cp.txt):target/classes
java -classpath ${CLASSPATH} com.practice.Server &
SERVER_PID=$$
while ! nc localhost 1111 > /dev/null 2>&1 < /dev/null; do
echo "$(date) - waiting for server at localhost:1111..."
sleep 1
done
java -classpath ${CLASSPATH} com.practice.Client
CLIENT_PID=$$
cleanup
But I keep getting
waiting for server at localhost:1111
with error :
Error: Could not find or load main class com.paractice.Server
Note : This sh file is present in project folder i.e. parallel to src and target folder.
Please help !!!
Upvotes: 3
Views: 1415
Reputation: 4221
You could use the exec
plugin.
Instead of:
mvn test dependency:build-classpath -Dmdep.outputFile=cp.txt
CLASSPATH=$(cat cp.txt):target/classes
java -classpath ${CLASSPATH} com.practice.Server &
you can do:
mvn test exec:java -Dexec.mainClass=com.practice.Server
This will run your program synchronously.
You can change that by either adding the ampersand (&
) at the end
or
use mvn exec:exec
, which can run asynchronously:
mvn test exec:exec -Dexec.async=true -Dexec.executable="java" -Dexec.args="-classpath %classpath com.practice.Server"
Full description of the plugin is available at http://www.mojohaus.org/exec-maven-plugin/index.html
Upvotes: 2