0

I have a .jar file on my desktop. In order to start it I have to type a certain command in the CMD after going to my desktop using cd desktop. Currently I do this:

  1. Open cmd and have C:\Users\Name
  2. type cd desktop and get C:\Users\Name\Desktop
  3. type the command that starts the .jar file

I am wondering is it possible to make a shortcut that would do all of this in a click without me having to do the aforementioned stuff. Thanks.

Pac0
  • 21,465
  • 8
  • 65
  • 74
Onyx
  • 5,186
  • 8
  • 39
  • 86

1 Answers1

4

Save below into a .bat file, change if needed, and then you can use it to start the jar file.

::Uncomment below to hide the commands
::@echo off
cd /d "C:\Users\%USERNAME%\Desktop"
::Below is the line starts the .jar file, change it to your real one:
java -jar file.jar

You can also change the last line to start "" java -jar file.jar which won't leave the batch file window open there.

In above example, java executable folder should be in the PATH environment variable.
If it's not, either add it to PATH and reopen the batch file, or add a line:

cd /d "Drive:\path\to\java\executable\"

above the java line.

Til
  • 5,150
  • 13
  • 26
  • 34
  • 2
    Also, it might be useful to add that the java executable folder must be in the `PATH` environment variable. It should already be the case with a regular installation, but if it is not the case, it could be something puzzling for beginners. – Pac0 Feb 15 '19 at 08:56
  • @Pac0 Yeah you are right, updated :) – Til Feb 15 '19 at 09:30
  • 1
    You could replace `Name` by `%USERNAME%`. That way it always works on other sessions. –  Feb 18 '19 at 03:49
  • @w1ll Yeah, that's a good idea, updated, thanks :-) – Til Feb 18 '19 at 03:53
  • The executable path can be added to the start line like this: `start "" /D "Drive:\path\to\java\executable" java -jar file.jar`. However, for me on Windows 10, this does not close the Java command window, so you still end up with an extra unneeded window. You can avoid that by using `javaw -jar file.jar` instead of `java -jar file.jar`. – Ryan Jan 28 '21 at 14:16