0

I have a command like this:

D:\ab cd\jre\bin\javaw -jar path_to_my_jar

I want to be able to execute this command in a batch file, so I tested it first on the command line with:

start "test" "D:\ab cd\jre\bin\javaw -jar path_to_my_jar"

However, it threw me an error saying:

Windows cannot find D:\ab cd\jre\bin\javaw -jar path_to_my_jar
make sure you typed the name correctly and try again

What is the right command to make it work ?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Abbas
  • 3,144
  • 2
  • 25
  • 45
  • 4
    `start "test" "D:\ab cd\jre\bin\javaw" -jar "path_to_my_jar"` – Til Feb 19 '19 at 15:01
  • There is no point in using a title ("test") for the start command. Also, use the start command only if you want the batch file to continue and not wait for the java application to complete. – Klitos Kyriacou Feb 19 '19 at 15:35
  • 3
    @KlitosKyriacou, it is highly recommended to provide a title (even it it's empty), because `start` may interpret the first quoted string as a title, which is `D:\ab cd\jre\bin\jawaw` in this situation otherwise; providing an explicit title avoids trouble because of that "feature" of `start`... – aschipfl Feb 19 '19 at 16:24
  • 1
    Abbas, it is usually good to accept the best answers which contain the most useful explanation for other readers. You accepted an answer which contained just code (but now edited). But please, in the future, consider accepting answers with explanations and upvote the other answers, too if you haven't. – double-beep Feb 19 '19 at 17:32
  • @double-beep I accepted that since it was the first response and it worked for me. But I'll surely consider what you said the next time. Also, I upvoted other answers as well. Thanks – Abbas Feb 20 '19 at 09:12

3 Answers3

3

When your path contains spaces, you have to wrap it in quotation marks. But, you only have to wrap the path itself, not the parameters sent (otherwise Windows will treat the parameters as part of the path.)

start "test" "D:\ab cd\jre\bin\javaw" -jar path_to_my_jar
Tobb
  • 11,850
  • 6
  • 52
  • 77
2

There are two ways to do that:

start "test" "D:\ab cd\jre\bin\javaw" -jar path_to_my_jar

also mentioned here and here and even:

start "test" D:\ab cd\jre\bin\javaw -jar path_to_my_jar

which should also work fine if the path doesn't contain spaces. In your question, it won't work.

The reason your command didn't work was because the system thought that you wanted to open a file whose full path was D:\ab cd\jre\bin\javaw -jar path_to_my_jar.

See start /? in cmd for more information.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • It may also be useful to point out that the `start` command is only needed if you want the batch file to continue without waiting for the Java application to terminate. Also, there is no point in having a title ("test") for the `start` command since it starts a GUI-only (non-console) application. – Klitos Kyriacou Feb 19 '19 at 15:38
2

As I first commented, this would work:

start "test" "D:\ab cd\jre\bin\javaw" -jar "path_to_my_jar"  

But I would suggest change to this to remove the ambiguity (potential danger):

start "title" "D:\ab cd\jre\bin\javaw.exe" -jar "path_to_my_jar"

Also if you do this often, I would suggest you to add D:\ab cd\jre\bin\ to PATH variable,
then afterwards you can just start "title" javaw ....

Path that has spaces inside need to be enclosed.
On the other hand, the things enclosed by a pair of quotes are considered as a whole.

And as Klitos Kyriacou said in the comments to double-beep's answer, the start "test" part is optional in this case, seems superfluous, you can just "path\to\javaw.exe" -jar "path_to_my_jar".
However, I wouldn't suggest you to remove it. As it's not intuitive to know which exe won't wait and which will, so it's actually better to keep using start to control it intuitively.
start "" won't pause current batch/cmd to wait, but start /wait will.
Related question:
How does the command prompt know when to wait for exit?

Til
  • 5,150
  • 13
  • 26
  • 34
  • 1
    Have +1 from me. However, I wouldn't call that 'ambiguity' because IMO it is dangerous as there may be e.g. a `javaw.txt` file in this directory. – double-beep Feb 19 '19 at 15:33