Run .jar from batch-file

JavaFileBatch File

Java Problem Overview


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

Java Solutions


Solution 1 - Java

On Windows you can use the following command.

start javaw -jar JarFile.jar

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

Solution 2 - Java

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 )

Solution 3 - Java

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.

Solution 4 - Java

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"

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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

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

Solution 9 - Java

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.

Solution 10 - Java

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.

Solution 11 - Java

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:

    maven-dependency-plugin ${version.plugin.maven-dependency-plugin} copy-dependencies package copy-dependencies ${project.build.directory}/lib false true runtime maven-jar-plugin ${version.plugin.maven-jar-plugin} true lib/ true true

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

Solution 12 - Java

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

Solution 13 - Java

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.

Solution 14 - Java

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

Solution 15 - Java

you shoult try this one :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArivu2020View Question on Stackoverflow
Solution 1 - JavaJleuleuView Answer on Stackoverflow
Solution 2 - JavaBrunoMedeirosView Answer on Stackoverflow
Solution 3 - JavaNagarajan S RView Answer on Stackoverflow
Solution 4 - JavaJonno_FTWView Answer on Stackoverflow
Solution 5 - JavaUser123456View Answer on Stackoverflow
Solution 6 - JavamdmView Answer on Stackoverflow
Solution 7 - JavaRichieHindleView Answer on Stackoverflow
Solution 8 - JavaVenkatView Answer on Stackoverflow
Solution 9 - JavabosariView Answer on Stackoverflow
Solution 10 - Javamerlin2011View Answer on Stackoverflow
Solution 11 - JavarazvanoneView Answer on Stackoverflow
Solution 12 - JavaBalusCView Answer on Stackoverflow
Solution 13 - JavaJitenSView Answer on Stackoverflow
Solution 14 - Javaganesh vechalapuView Answer on Stackoverflow
Solution 15 - JavaBurhan ARASView Answer on Stackoverflow