[java] Run .jar from batch-file

I have created an executable .jar file. How can I execute the .jar using a batch-file without mentioning a class path?

This question is related to java file batch-file

The answer is


On Windows you can use the following command.

start javaw -jar JarFile.jar

By doing so, the Command Prompt Window doesn't stay open.


Just the same way as you would do in command console. Copy exactly those commands in the batch file.


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.


java -jar "C:\\myjarfile.jar"

You might need to add "\\" to the command. Try this!


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"

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 )


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 :)


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.


you shoult try this one :

java -cp youJarName.jar your.package.your.MainClass

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.


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.


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.


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.


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.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)