简体   繁体   English

从批处理文件运行.jar

[英]Run .jar from batch-file

I have created an executable .jar file. 我创建了一个可执行的.jar文件。 How can I execute the .jar using a batch-file without mentioning a class path? 如何在不提及类路径的情况下使用批处理文件执行.jar

On Windows you can use the following command. 在Windows上,您可以使用以下命令。

start javaw -jar JarFile.jar

By doing so, the Command Prompt Window doesn't stay open. 通过这样做,命令提示符窗口不会保持打开状态。

There is a solution to this that does not require to specify the path of the jar file inside the .bat. 有一个解决方案,不需要在.bat中指定jar文件的路径。 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. 这意味着只要.bat文件始终位于与jar相同的目录中,就可以在文件系统中移动jar而不进行任何更改。 The .bat code is: .bat代码是:

java -jar %~dp0myjarfile.jar %*

Basically %0 would expand to the .bat full path, and %~dp0 expands to the .bat full path except the filename. 基本上%0将扩展到.bat完整路径, %~dp0扩展到除文件名之外的.bat完整路径。 So %~dp0myjarfile.jar is the full path of the myjarfile.jar colocated with the .bat file. 所以%~dp0myjarfile.jar是与.bat文件%~dp0myjarfile.jar的完整路径。 %* will take all the arguments given to the .bat and pass it to the Java program. %*将获取给.bat的所有参数并将其传递给Java程序。 (see: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true ) (参见: http//www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr = true

如果您希望批处理文件运行jar文件,请创建一个名为runjava.bat的空白文件,其中包含以下内容:

java -jar "C:\myjarfile.jar"

You can create a batch file with .bat extension with the following contents 您可以使用以下内容创建扩展名为.bat的批处理文件

Use java for .jar that does not have UI and is a command line application 使用java for .jar ,它没有UI,是一个命令行应用程序

@ECHO OFF
start java -jar <your_jar_file_name>.jar

Use javaw for .jar that has a UI javaw用于具有UI的.jar

@ECHO OFF
start javaw -jar <your_jar_file_name>.jar

Please make sure your JAVA_HOME is set in the environment variables. 请确保您的JAVA_HOME已在环境变量中设置。

cd "Your File Location without inverted commas"

example : cd C:\\Users*****\\Desktop\\directory\\target 示例: cd C:\\ Users ***** \\ Desktop \\ directory \\ target

java -jar myjar.jar

example bat file looks like this: 示例bat文件如下所示:

@echo OFF
cd C:\Users\****\Desktop\directory\target
java -jar myjar.jar

This will work fine. 这样可以正常工作。

To run a .jar file from the command line, just use: 要从命令行运行.jar文件,只需使用:

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 : 要将其作为批处理文件执行,只需将命令复制到文本文件并将其另存为.bat

@echo off
java -jar YourJar.jar

The @echo off just ensures that the second command is not printed. @echo off只确保不打印第二个命令。

If double-clicking the .jar file in Windows Explorer works, then you should be able to use this: 如果在Windows资源管理器中双击.jar文件,则应该可以使用:

start myapp.jar

in your batch file. 在您的批处理文件中。

The Windows start command does exactly the same thing behind the scenes as double-clicking a file. Windows start命令在后台执行与双击文件完全相同的操作。

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

You might need to add "\\\\" to the command. 您可能需要在命令中添加"\\\\" Try this! 试试这个!

You need to make sure you specify the classpath in the MANIFEST.MF file. 您需要确保在MANIFEST.MF文件中指定类路径。 If you are using Maven to do the packaging, you can configure the following plugins: 如果您使用Maven进行打包,则可以配置以下插件:

1. maven-depedency-plugin: 1. maven-depedency-plugin:
2. maven-jar-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: 生成的清单文件将打包在META-INF下的可执行jar中,如下所示:

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: Windows脚本如下所示:

@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 :) 这应该是使用Maven构建可执行jar的完整配置:)

My understanding of the question is that the OP is trying to avoid specifying a class-path in his command line. 我对这个问题的理解是OP试图避免在他的命令行中指定一个类路径。 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 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: 要使用名为MANIFEST的清单文件创建jar,可以使用以下命令:

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. 如果你指定相对的类路径(即同一目录中的其他jar),那么你可以移动jar,mdm的答案中提到的批处理文件仍然有效。

Just the same way as you would do in command console. 就像在命令控制台中一样。 Copy exactly those commands in the batch file. 准确复制批处理文件中的那些命令。

you can use the following command in the .bat file newly created: 您可以在新创建的.bat文件中使用以下命令:

@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. 如果系统中安装了多个java版本,请提供java的路径,并确保在创建jar文件时指定了main方法和manifest文件。

Steps 1- Create/export a runnable jar file out of your project. 步骤1-从项目中创建/导出可运行的jar文件。

2- Create a .bat file with the below content 2-使用以下内容创建.bat文件

@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. 如果您正在使用任何外部jar,则需要3- set classpath。

4- Put the .bat file and jar file in the same folder. 4-将.bat文件和jar文件放在同一文件夹中。

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. 5-根据java -cp命令,给出确切的jar文件位置和main方法的完全限定名称,然后根据需要跟随参数列表。

inside .bat file format 里面的.bat文件格式

SET CLASSPATH=%Path%; SET CLASSPATH =%Path%;

-------set java classpath and give jar location-------- set classpath=%CLASSPATH%;../lib/MoveFiles.jar; -------设置java classpath并给出jar位置-------- 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 ---------提到要运行的java类的完全分类名称,这是在jar中给出的------ Java com.mits.MoveFiles pause

你要试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM