首页 > 解决方案 > How to create a single jar from multiple packages?

问题描述

I want to create a single jar file from multiple packages. I have created the jar using below command but when I'm importing it to a project as a dependency it is not working.

jar cfe output/jar/my-java.jar Main src/pkg1/pkg0/*.class src/pkg1/*.class src/pkg2/*.class

My project structure is something like below structure

src
  pkg1
    A.java
    B.JAVA
    pkg0
     E.java
   pkg2
    C.java
    D.java

My Example code is something like

import pkg1.A;

public class Main {
    public static void main(String[] args) {
        A.printMe("Hello World");
    }
}

error that I'm getting is:

java pkg1 not exist But in the editor(IntelliJ), it is not showing errors and also i'm able to import class but not package.

Note: I don't want to use maven.

标签: java

解决方案


An unzip -t something.jar shows the actual file structure of the jar file (zip). It is the same as the class structure of it (except that instead "/", a "." is the separator).

In your case, the problem will be that src will be on the top level, and not pkg1. Either import src.pkg1 (very dirty), or play a little bit more with the directories / jar flags.


推荐阅读