0

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?

4
  • 2
    You should use a backslash, not a forward slash: "%CUSTOM_JAVA_HOME%/bin/javaw" should be "%CUSTOM_JAVA_HOME%\bin\javaw" and set CP=myjar.jar;../lib/* should be set CP=myjar.jar;..\lib\*; Commented Jul 2, 2018 at 13:12
  • @a_horse_with_no_name using backslashes doesn't solve it. I still get the same error. Commented Jul 2, 2018 at 13:23
  • 3
    That's because Start sees "%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% Commented Jul 2, 2018 at 13:30
  • @Compo that worked!! Asweome. You should put that comment as the solution. Commented Jul 2, 2018 at 14:12

1 Answer 1

4

Start sees "%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%
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.