1

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

1 Answers1

1

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% ...
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thanks, that would work! But isn't there some array variable in CMD? Or maybe a variable holding several lines (like HERE-assignment in Unix shell and PHP/Perl scripts)? To improve the readability – Alexander Farber Jun 13 '16 at 07:58
  • 1
    @AlexanderFarber see here: http://stackoverflow.com/questions/17605767/create-list-or-arrays-in-windows-batch – Jens Jun 13 '16 at 07:59
  • Your trick with appending paths to the same variable `MYCLASSPATH` is actually so simple and good, that I could use it in my Unix shell script as well :-) – Alexander Farber Jun 13 '16 at 08:11
  • @JosefZ Thanks. Have changed it – Jens Jun 13 '16 at 08:11
  • `set "MYCLASSPATH=C:\Users\user1\slova\WebSockets\target\classes;"` contains trailing `;` semicolon. Then after `set MYCLASSPATH=%MYCLASSPATH%;%REPO%\…`, the `MYCLASSPATH` variable would contain **doubled** `;;` semicolon. (I don't know why my previous comment disappeared?) – JosefZ Jun 13 '16 at 08:52