I have created an executable .jar
file. How can I execute the .jar
using a batch-file without mentioning a class path?
-
9Dude, formatting, a question is not a code segment. – Jonno_FTW Apr 12 '10 at 12:40
15 Answers
On Windows you can use the following command.
start javaw -jar JarFile.jar
By doing so, the Command Prompt Window doesn't stay open.

- 1,931
- 1
- 16
- 13
-
Windows cannot find 'javaw'. I fixed this by just having `start JarFile.jar` (with the .jar file in the same folder as the batch file). – NiteCyper Nov 21 '14 at 13:39
-
1
-
when I use start I am getting "windows cannot find '-jar'. Make sure you type the name correctly..." – Navakanth Feb 27 '17 at 11:07
-
-
There is a solution to this that does not require to specify the path of the jar file inside the .bat. This means the jar can be moved around in the filesystem with no changes, as long as the .bat file is always located in the same directory as the jar. The .bat code is:
java -jar %~dp0myjarfile.jar %*
Basically %0
would expand to the .bat full path, and %~dp0
expands to the .bat full path except the filename. So %~dp0myjarfile.jar
is the full path of the myjarfile.jar colocated with the .bat file. %*
will take all the arguments given to the .bat and pass it to the Java program. (see: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true )

- 1,441
- 12
- 14
You can create a batch file with .bat
extension with the following contents
Use java
for .jar
that does not have UI and is a command line application
@ECHO OFF
start java -jar <your_jar_file_name>.jar
Use javaw
for .jar
that has a UI
@ECHO OFF
start javaw -jar <your_jar_file_name>.jar
Please make sure your JAVA_HOME
is set in the environment variables.

- 407
- 6
- 11

- 1,418
- 2
- 19
- 31
If you want a batch file to run a jar file, make a blank file called runjava.bat with the contents:
java -jar "C:\myjarfile.jar"

- 8,601
- 7
- 58
- 90
cd "Your File Location without inverted commas"
example : cd C:\Users*****\Desktop\directory\target
java -jar myjar.jar
example bat file looks like this:
@echo OFF
cd C:\Users\****\Desktop\directory\target
java -jar myjar.jar
This will work fine.

- 1,422
- 2
- 23
- 36

- 2,492
- 3
- 30
- 44
-
Why without inverted commas (quotation mark)? If the path has a space in the name, you need the inverted commas. – Rob Jan 30 '13 at 05:56
To run a .jar
file from the command line, just use:
java -jar YourJar.jar
To do this as a batch file, simply copy the command to a text file and save it as a .bat
:
@echo off
java -jar YourJar.jar
The @echo off
just ensures that the second command is not printed.

- 12,480
- 5
- 34
- 53
-
1@Carlos: I assumed that as the OP asked for a batch file, that they were using Windows. – mdm Apr 12 '10 at 14:20
-
I am using windows 7.I have the jar file in the D:\my.jar I write the following code in my batch file @echo off java -jar my.jar and also i tried @echo off java -jar D:\my.jar what can i do? – Arivu2020 Apr 13 '10 at 10:54
If double-clicking the .jar file in Windows Explorer works, then you should be able to use this:
start myapp.jar
in your batch file.
The Windows start
command does exactly the same thing behind the scenes as double-clicking a file.

- 272,464
- 47
- 358
- 399
-
-
@Arivu2020: Are you in the right directory? Perhaps your batch file need to do `cd c:\path\to\my\stuff` before trying to run the jar file? – RichieHindle Apr 13 '10 at 08:41
you can use the following command in the .bat file newly created:
@echo off
call C:\SWDTOOLS\**PATH\TO\JAVA**\java_1.7_64\jre\bin\java -jar workspace.jar
Please give the path of the java if there are multiple versions of java installed in the system and make sure you specified the main method and manifest file is created while creating the jar file.

- 1,922
- 1
- 19
- 38
You need to make sure you specify the classpath in the MANIFEST.MF file. If you are using Maven to do the packaging, you can configure the following plugins:
1. maven-depedency-plugin:
2. maven-jar-plugin:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>${version.plugin.maven-dependency-plugin}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.plugin.maven-jar-plugin}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
The resulting manifest file will be packaged in the executable jar under META-INF and will look like this:
Manifest-Version: 1.0
Implementation-Title: myexecjar
Implementation-Version: 1.0.0-SNAPSHOT
Built-By: razvanone
Class-Path: lib/first.jar lib/second.jar
Build-Jdk: your-buildjdk-version
Created-By: Maven Integration for Eclipse
Main-Class: ro.razvanone.MyMainClass
The Windows script would look like this:
@echo on
echo "Starting up the myexecjar application..."
java -jar myexecjar-1.0.0-SNAPSHOT.jar
This should be complete config for building an executable jar using Maven :)

- 1,351
- 18
- 27
My understanding of the question is that the OP is trying to avoid specifying a class-path in his command line. You can do this by putting the class-path in the Manifest file.
In the manifest:
Class-Path: Library.jar
This document gives more details:
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
To create a jar using the a manifest file named MANIFEST, you can use the following command:
jar -cmf MANIFEST MyJar.jar <class files>
If you specify relative class-paths (ie, other jars in the same directory), then you can move the jar's around and the batch file mentioned in mdm's answer will still work.

- 71,677
- 44
- 195
- 329
Just the same way as you would do in command console. Copy exactly those commands in the batch file.

- 1,082,665
- 372
- 3,610
- 3,555
Steps 1- Create/export a runnable jar file out of your project.
2- Create a .bat file with the below content
@Echo off
set classpath="c:\jars\lib\*****.jar;c:\jars\lib\*****.jar;c:\extJars\****.jar"
java -cp %classpath%;c:\apps\applName\yourJar.jar com.****.****.MainMethod args1 args2 ...
@pause
3- set classpath is required if any external jars you are using.
4- Put the .bat file and jar file in the same folder.
5- As per the java -cp command give your exact jar file location and the fully qualified name of the main method and followed by argument list as per requirement.

- 41
- 1
inside .bat file format
SET CLASSPATH=%Path%;
-------set java classpath and give jar location-------- set classpath=%CLASSPATH%;../lib/MoveFiles.jar;
---------mention your fully classified name of java class to run, which was given in jar------ Java com.mits.MoveFiles pause

- 1
- 1

- 9
- 2
you shoult try this one :
java -cp youJarName.jar your.package.your.MainClass

- 2,517
- 25
- 19