Java 创建 .jar 文件

发布于 2024-10-10 05:02:07 字数 268 浏览 0 评论 0原文

我正在学习Java,但我遇到了一个问题。我创建了 6 个不同的类,每个类都有自己的 main() 方法。我想为每个类创建可执行的 .jar ,即 6 个可执行的 .jar 文件。

到目前为止,我尝试过

java -jar cf myJar.jar myClass.class

,但得到了“无法访问 jarfile cf”。我做错了什么,但我不知道是什么。如果这意味着什么的话,我也在使用 Eclipse IDE。

I'm learning Java and I have a problem. I created 6 different classes, each has it's own main() method. I want to create executable .jar for each class, that is 6 executable .jar files.

So far I tried

java -jar cf myJar.jar myClass.class

and I get 'Unable to access jarfile cf'. I'm doing something wrong but I don't know what. I'm also using Eclipse IDE if that means something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

鱼忆七猫命九 2024-10-17 05:02:07

为了创建 .jar 文件,您需要使用 jar 而不是 java

jar cf myJar.jar myClass.class

此外,如果您想让它可执行,您需要指定 应用程序的入口点(即具有 public static void main(String[] args) 的类)。这通常是通过创建包含 manifest 文件来完成code>Main-Class 标头(例如,Main-Class: myClass)。

然而,正如 Mark Peters 指出的,使用 JDK 6,您可以使用 e 选项来定义入口点:

jar cfe myJar.jar myClass myClass.class 

最后,您可以执行它:

java -jar myJar.jar

另请参阅

In order to create a .jar file, you need to use jar instead of java:

jar cf myJar.jar myClass.class

Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class header (e.g., Main-Class: myClass).

However, as Mark Peters pointed out, with JDK 6, you can use the e option to define the entry point:

jar cfe myJar.jar myClass myClass.class 

Finally, you can execute it:

java -jar myJar.jar

See also

霊感 2024-10-17 05:02:07

正如您提到的,您正在使用 Eclipse...只要您运行过一次具有 main 的每个类,Eclipse 就可以为您创建 JAR。右键单击该项目并单击导出,然后选择 Java 文件夹下的“Runnable JAR file”。在启动配置中选择类名,选择保存 jar 的位置,并在必要时决定如何处理库。点击完成,在裤子上擦手。

Sine you've mentioned you're using Eclipse... Eclipse can create the JARs for you, so long as you've run each class that has a main once. Right-click the project and click Export, then select "Runnable JAR file" under the Java folder. Select the class name in the launch configuration, choose a place to save the jar, and make a decision how to handle libraries if necessary. Click finish, wipe hands on pants.

安静 2024-10-17 05:02:07

通常,您需要在清单中添加的内容比使用 -e 开关获得的内容要多,在这种情况下,语法为:

jar -cvfm myJar.jar myManifest.txt myApp.class

其中内容为:“create verbose jarFilename manifestFilename”,后跟您要的文件想要包括在内。

请注意,您提供的清单文件的名称可以是任何名称,因为 jar 会自动重命名它并将其放入 jar 文件中的正确位置。

Often you need to put more into the manifest than what you get with the -e switch, and in that case, the syntax is:

jar -cvfm myJar.jar myManifest.txt myApp.class

Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include.

Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right place within the jar file.

抽个烟儿 2024-10-17 05:02:07

方式1:

让我们有java文件test.java,其中包含主类testa
现在首先我们将 java 文件简单地编译为 javac test.java
我们在同一目录中创建文件 manifest.txt 并编写 Main-Class: mainclassname 。例如:

  Main-Class: testa

然后我们通过以下命令创建 jar 文件:

  jar cvfm anyname.jar manifest.txt testa.class

然后我们通过以下命令运行 jar 文件:java -jar anyname.jar

方式 2:

让我们有一个名为 1 的包,每个类都在其中。
然后我们通过这个命令创建jar文件:

  jar cf anyname.jar one

然后我们在anyname.jar文件中的META-INF目录中打开manifest.txt并

  Main-Class: one.mainclassname 

在第三行写入,然后我们通过这个命令运行jar文件:

  java -jar anyname.jar

使jar文件具有多个类文件:jar cf anyname.jar one.class 二.class 三.class......

way 1 :

Let we have java file test.java which contains main class testa
now first we compile our java file simply as javac test.java
we create file manifest.txt in same directory and we write Main-Class: mainclassname . e.g :

  Main-Class: testa

then we create jar file by this command :

  jar cvfm anyname.jar manifest.txt testa.class

then we run jar file by this command : java -jar anyname.jar

way 2 :

Let we have one package named one and every class are inside it.
then we create jar file by this command :

  jar cf anyname.jar one

then we open manifest.txt inside directory META-INF in anyname.jar file and write

  Main-Class: one.mainclassname 

in third line., then we run jar file by this command :

  java -jar anyname.jar

to make jar file having more than one class file : jar cf anyname.jar one.class two.class three.class......

海未深 2024-10-17 05:02:07

将所有 6 个类放入 6 个不同的项目中。然后创建所有6个项目的jar文件。这样你就会得到6个可执行的jar文件。

Put all the 6 classes to 6 different projects. Then create jar files of all the 6 projects. In this manner you will get 6 executable jar files.

紫南 2024-10-17 05:02:07

如果您不想手动处理 Manifest 文件,可以使用“e”选项。这明确定义了应用程序中的入口点。

jar vcfe anyname.jar EntryPoint EntryPoint.class <files to include>

就我而言,它看起来像这样

jar vcfe people.jar Peer Peer.class ClientThread.class transport/*

结果,我能够运行我的程序

java -jar people.jar 

If you don't want to manually deal with the Manifest file you can use the "e" option. This explicitly defines the entry point in your application.

jar vcfe anyname.jar EntryPoint EntryPoint.class <files to include>

In my case it looked like this

jar vcfe people.jar Peer Peer.class ClientThread.class transport/*

As a result, I was able to run my program with

java -jar people.jar 
野の 2024-10-17 05:02:07

用于搜索所有 Spring Boot 微服务 jar 文件创建的文件夹的目录

搜索目录并运行 Maven 命令

 rootDir="D:/project/backend"
    for dir in "$rootDir"/*/; do
        if [[ -f "$dir/pom.xml" ]]; then
            echo "Running 'mvn clean package' in $dir"
            (cd "$dir" && mvn clean package)
        else
            echo "Skipping $dir - No pom.xml found"
        fi
    done

Directory to search for folders all spring boot microservice jar file create

Search for directories and run Maven command

 rootDir="D:/project/backend"
    for dir in "$rootDir"/*/; do
        if [[ -f "$dir/pom.xml" ]]; then
            echo "Running 'mvn clean package' in $dir"
            (cd "$dir" && mvn clean package)
        else
            echo "Skipping $dir - No pom.xml found"
        fi
    done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文