0

I define variable like:

set LOGGING_MANAGER=-Djuli-logback.configurationFile=file:"%CATALINA_HOME%\conf\logback.xml"

Notice, that I wrap %CATALINA_HOME%\conf\logback.xml in double quotes, because path may contain spaces. And at execution step this variable will substitute to java program.

Example of substitution:
path_to_jre/java.exe %LOGGING_MANAGER%

Next code I can't change: (it's from catalina.bat file from Apache Tomcat)

if not "%LOGGING_MANAGER%" == "" ...

This if statement will fail, because variable contains quotes.

There I have one of 2 problems:

  1. if statement fail (error occured).
  2. without quotes when substitute to java program have problems (space is delimiter).

How can I change variable definition so that the path may contain spaces and if statement will work fine?

B.shruti
  • 1,589
  • 1
  • 21
  • 41
learp
  • 153
  • 8
  • 4
    `if defined LOGGING_MANAGER` ? – MC ND Apr 04 '17 at 08:33
  • I can't change that code :( I want change variable defenition – learp Apr 04 '17 at 08:36
  • @MCND forget to mention you. – learp Apr 04 '17 at 08:53
  • 1
    Please [edit] your question and add a [mcve] of how do you supply `%LOGGING_MANAGER%` to java _call_. – JosefZ Apr 04 '17 at 08:56
  • @JosefZ I add example. It's not all parametrs, but I think it's enough to understand the problem. – learp Apr 04 '17 at 09:05
  • I'm voting to close this question as off-topic because it's unsolvable in the scope of the question –  Apr 04 '17 at 10:00
  • `set "LOGGING_MANAGER=-Djuli-logback.configurationFile=file:""%CATALINA_HOME%\conf\logback.xml"""` then `"%LOGGING_MANAGER%"` would be taken as one parameter without surrounding double quotes if supplied to a `c++` or `c#` console application (I don't have `java` installed). Note doubled inner `"` as `""` in above `set` command (uses `set "var=value"` syntax pattern). And `if not "%LOGGING_MANAGER%" == "" ...` should work as well. – JosefZ Apr 04 '17 at 10:23
  • Can you at least modify the `java.exe` invocation line? – MC ND Apr 04 '17 at 10:31
  • @MCND it's just example :) – learp Apr 04 '17 at 11:44

2 Answers2

1
for %%a in ("%CATALINA_HOME%\conf\logback.xml") do set "LOGGING_MANAGER=-Djuli-logback.configurationFile=file:%%~sa"

should set logging_manager appropriately so that you don't have to "quote the filename"


for %%a in ("%CATALINA_HOME%\conf") do net use o: "\\%userdomain%\sharenameforthedrivewherecatalina_homeisresident%%~pa"

set "LOGGING_MANAGER=-Djuli-logback.configurationFile=file:o:\conf\logback.xml"

may also work - if you know the share name for the drive where catalina_home is resident and assuming o: is an unused drive.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I think that two problems with this solution (nice one, good point) could be : **a**) there are not short names (can be configured) **b**) the OS decides that some intermediate folder does not need a short name generated but it includes a special character. – MC ND Apr 04 '17 at 11:39
  • @Magoo thanks for the answer, but it's not working :( Behaves as if I will define variable as set LOGGING_MANAGER=...%CATALINA_HOME%\conf\logback.xml (without quotes) – learp Apr 04 '17 at 11:51
  • @Magoo echo after your definition print -Djuli-logback.configurationFile=file:C:\x y z\Tomcat\tomcat\conf\logback.xml (without quotes or anything) – learp Apr 04 '17 at 11:53
  • It would appear that your system has been set up to not generate shortnames (non-standard configuration). You would need to discuss this with your systems administrator. – Magoo Apr 04 '17 at 11:57
1

note: Not tested, but after reading the catalina.bat this is a way to deal with the problem.

If you are running this from a batch file, you can use

set LOGGING_MANAGER_FILE="%CATALINA_HOME%\conf\logback.xml"
set LOGGING_MANAGER=-Djuli-logback.configurationFile=file:%%LOGGING_MANAGER_FILE%%
set _RUNJAVA=CALL "%JRE_HOME%\bin\java.exe"

call catalina.bat

What it does is

  • Save the file name with quotes in a separate variable
  • Set the LOGGING_MANAGER variable using a escaped reference of the previous variable. As it will not be expanded, the final content of the LOGGING_MANAGER variable is -Djuli-logback.configurationFile=file:%LOGGING_MANAGER_FILE%

That way, when the if "%LOGGING_MANAGER%"=="" ... is executed, there will be not any problematic quote.

And now the set _RUNJAVA line. Inside catalina.bat, the contents of the _RUNJAVA are used to initialize the _EXECJAVA variable that will launch the server. _RUNJAVA is initialized in setclasspath.bat IF it has not been initialized by the user (code from setclasspath.bat).

rem Don't override _RUNJAVA if the user has set it previously
if not "%_RUNJAVA%" == "" goto gotRunJava
rem Set standard command for invoking Java.
rem Also note the quoting as JRE_HOME may contain spaces.
set _RUNJAVA="%JRE_HOME%\bin\java.exe"

The default value is "%JRE_HOME%\bin\java.exe", we just add a CALL batch command before to force a second iteration of the batch parser that will expand the quoted %LOGGING_MANAGER_FILE% variable in the final command line.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • thank you very much! I found EnableDelayedExpansion parameter, maybe it will help and it will be easy? I try it a little bit latter. – learp Apr 04 '17 at 13:11