You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法创建并运行JAR文件:Java类打包操作问题求助

Fixing JAR Creation & Execution Issues for Your Java Project

Hey there, let's break down where things went wrong with your JAR creation and get your code running smoothly.

First, Let's Spot the Problems in Your Current Workflow

Your code is correct, but your compilation and JAR packaging steps are missing key details related to Java's package structure:

  1. When you ran javac *.java, you didn't generate the proper directory structure for the company package. Java requires compiled class files to live in a directory matching their package name (so Employee.class needs to be inside a company/ folder).
  2. Your jar command only packaged .class files in the current directory, which means the company package structure wasn't included in the JAR. This would cause classpath errors when trying to run the JAR later.
  3. If you want a runnable JAR, you also need to specify the main class (PrintEmployee) in a manifest file—your current command doesn't do that.

Step-by-Step Correct Workflow

1. First, Organize Your Files Properly

Make sure your project has this directory structure:

your-project-folder/
├─ company/
│  └─ Employee.java
└─ PrintEmployee.java

2. Compile the Code with Proper Package Structure

Run this command from your project root folder to compile and generate the correct directory structure:

javac -d . company/Employee.java PrintEmployee.java

The -d . flag tells javac to place compiled class files in the current directory, which will automatically create the company/ folder and put Employee.class inside it, while PrintEmployee.class stays in the root.

3. Create a Manifest File for Runnable JAR

If you want to run the JAR directly, create a file named MANIFEST.MF in your project root with this content (make sure to add a blank line at the end—Java requires this):

Main-Class: PrintEmployee

This tells the JVM which class contains the main() method to execute.

4. Package the JAR Correctly

Run this command to create the runnable JAR, including the package structure and manifest:

jar -cmf MANIFEST.MF company.jar company/*.class PrintEmployee.class

Let's break down the flags:

  • -c: Create a new JAR file
  • -m: Use the specified manifest file (MANIFEST.MF)
  • -f: Specify the output JAR filename (company.jar)
  • The last arguments are the files/folders to include: the company package's class files and the PrintEmployee class.

5. Run the JAR

Now you can execute the JAR with:

java -jar company.jar

You should see the output 10000 as expected!


If You Just Need a Non-Runnable JAR (For Library Use)

If you don't need the JAR to be executable, you can skip the manifest file and run:

jar -cf company.jar company PrintEmployee.class

This packages the full company directory and PrintEmployee.class into the JAR, preserving the package structure needed for other code to reference the Employee class.

内容的提问来源于stack exchange,提问作者Shinghim LEE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:57:04