Alexander Farber Alexander Farber - 5 months ago 19
Java Question

Concatenate a list of paths to a variable - to pass to "java -classpath ..." in CMD batch script

I am trying to port a simple shell script starting a Java program to a CMD batch script for Windows:

@echo on

set REPO="C:\Users\user1\.m2\repository"
set VERSION=9.3.9.v20160517

"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath C:\Users\user1\slova\WebSockets\target\classes;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;%REPO%\org\eclipse\jetty\websocket\websocket-server\%VERSION%\websocket-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-common\%VERSION%\websocket-common-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-io\%VERSION%\jetty-io-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-client\%VERSION%\websocket-client-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-servlet\%VERSION%\jetty-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-security\%VERSION%\jetty-security-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-server\%VERSION%\jetty-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-http\%VERSION%\jetty-http-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-servlet\%VERSION%\websocket-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-api\%VERSION%\websocket-api-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util-ajax\%VERSION%\jetty-util-ajax-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util\%VERSION%\jetty-util-%VERSION%.jar;%REPO%\org\postgresql\postgresql\9.4.1208.jre7\postgresql-9.4.1208.jre7.jar de.afarber.websockets.MyHandler


As you can see (if you scroll the above code to the right) there is a longer list of file paths following the
java -classpath
string.

Is it please possible to list the paths each on a separate line - and then concatenate that list by the means of CMD shell to a variable (adding semicolon
;
inbetween)?

That way I could better maintain my batch file (easier to edit in editor) and would finally just call
java -classpath %CPATHS% de.afarber.websockets.MyHandler


UPDATE:

If all JAR-files would be located in the same dir, I could have used the new Java 8 wildcard syntax
java -classpath "\that\dir\*" de.afarber.websockets.MyHandler
- but that wasn't the case here.

Answer

You can use a variable in this form:

set MYCLASSPATH=C:\Users\user1\slova\WebSockets\target\classes
set MYCLASSPATH=%MYCLASSPATH%;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar
...

"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath %MYCLASSPATH% ...