如何在Java中通过命令提示符运行JAR文件?
为了打包类文件,Java提供了一种称为JAR(Java归档)的文件格式。通常,JAR文件包含.class文件、图像、文本文件以及执行应用程序或库所需的库。
此文件格式用于分发Java中的应用程序软件和库。所有预定义的库都以这种格式提供。
如果您有任何以这种格式提供的库要在您的应用程序中使用它,您需要将其放在项目的当前(或lib)文件夹中,或者您需要为该特定的JAR文件设置类路径。
创建Jar文件
Java提供jar命令来处理jar文件,如果您在命令提示符中执行它,您将获得此命令的执行语法和选项,如下所示:
C:\>jar
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-n perform Pack200 normalization after creating a new archive
-e specify application entry point for stand-alone application bundled into an executable jar file
-0 store only; use no ZIP compression
-P preserve leading '/' (absolute path) and ".." (parent directory) components from file names
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file您可以使用带选项c、v、f的命令来创建JAR文件。以下是使用命令提示符创建JAR文件的语法:
jar cvf jar_file_name output_path
示例
在路径**D:/example**中创建一个名为Sample.java的Java文件,并将以下程序复制粘贴到其中:
public class Sample {
public static void main(String args[]){
System.out.println("Hi welcome to Tutorialspoint");
}
}使用javac命令编译上述程序,如下所示:
javac Example.java
如果您的程序在没有错误的情况下执行,则将在当前文件夹中生成.class文件。现在,为生成的类创建一个jar文件,如下所示:
C:\Sample>jar cvf sample.jar *.class added manifest adding: Sample.class(in = 434) (out= 302) (deflated 30%) Once you execute a JAR file is generated with the specified name.
使用eclipse创建JAR文件
您也可以使用IDE创建JAR文件。要使用eclipse创建JAR文件,请按照以下步骤操作:
打开eclipse,在其中创建一个项目,如下所示:

右键单击项目文件夹,并选择**导出**选项,如下所示:

在Java类别下选择JAR文件。

单击**下一步**。
输入JAR文件名和文件夹。
单击**完成**。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP