I'm writing a windows batch script to start my Java program in the background, using javaw.
The issue comes when I let the user have a custom path to the Java Home. Let's say in an example when the path to Java home is: C:\Users\Sample User\Desktop\java (notice the space in the path), when I try running the start command, it breaks because of the spaces.
Example:
@echo off
set CUSTOM_JAVA_HOME=C:\Users\Federico Einhorn\Desktop\java
set CP=myjar.jar;../lib/*;.
set JAVA_PARAMS=-myOption -Xmx1024M -classpath %CP%
set JAVA_CLASS=com.myorg.MyClass
set RUN_OPTS=%JAVA_PARAMS% %JAVA_CLASS% start
start /b "%CUSTOM_JAVA_HOME%/bin/javaw" %RUN_OPTS%
That start command fails as the CUSTOM_JAVA_HOME has spaces.
Windows cannot find '-myOption'. Make sure you typed the name correctly, and then try again.
This doesn't happen when I run my jar with regular java:
"%CUSTOM_JAVA_HOME%/bin/java" %RUN_OPTS%
Is there a way to solve this?
"%CUSTOM_JAVA_HOME%/bin/javaw"should be"%CUSTOM_JAVA_HOME%\bin\javaw"andset CP=myjar.jar;../lib/*should beset CP=myjar.jar;..\lib\*;Startsees"%CUSTOM_JAVA_HOME%/bin/javaw"as a title. The fix is to include a title yourself, even a blank one:Start "" /B "%CUSTOM_JAVA_HOME%\bin\javaw" %RUN_OPTS%