javacommand-linejar

Adding a file to a specific folder inside a jar


Quick question (sorry struggling with time)

How can I add any file from my file system under WEB-INF/classes folder in a jar file? I am trying this with jar command but it copies a file in a jar with its absolute path meaning if I create a file at E:\folder1\WEB-INF\classes folder and try to add it to a jar it creates folder1/WEB-INF/classes folder inside the jar and places the file into it.

I used jar command as below

jar -uvf E:\folder1\sample.jar E:\folder1\WEB-INF\classes\pkfe


Solution

  • you need to specifiy the -C-switch and alter the file-argument like so:

    jar -uvf E:\folder1\sample.jar -C E:\folder1 WEB-INF\classes\pkfe
    

    This makes the command run within E:\folder1 and include the files relative from that location

    You can also add entire folders this way:

    jar -uvf -uvf E:\folder1\sample.jar -C E:\folder1 .
    

    The . tells the command to add everything at the location of the working directory (E:\folder1). You need to move your sample.jar to somewhere else, otherwise you would softlock the command, as it would be adding itsself to itsself into eternity...

    Several specific files within a folder would work like this

    jar -uvf E:\folder1\sample.jar -C E:\folder1 WEB-INF\classes\pkfe -C E:\folder1 WEB-INF\classes\secondFile.dat -C E:\folder1 org\blupp\blah\thirdFile.class
    

    Source: jar -?